Kotlin Collections for Interview

Alex Manhooei
3 min readJan 20, 2022

Recently I’ve been looking at the language trends used during coding interviews and what stood out based on my observation was Java, Python, and Javascript for the most part. Based on what role you’re interviewing for, the choice of language will tell a lot about you. For example, picking Python as a language for solving a coding challenge when you’re applying for a Spring Application developer isn’t the best choice. The interviewer wants to assess your problem-solving capabilities and understand how familiar you’re with the programming language you will be using should you get hired for the job. There it makes sense to pick a language that suits the situation.

This brings us to another dilemma. Java is way more verbose than, let’s say, Python. Solving a coding challenge with Python is much simpler, and you can move faster with less typing (saving time). So you may ask, how can we demonstrate our Java strength while being fast and nimble? The answer is you can use a JVM language like Kotlin since it’s the closest cousin of Java. You still get to show off your coding skills in a JVM-based language but take advantage of the shortcuts and functional style Kotlin offers.

In this article, I’ll teach you how to use the top 5 data structures you may need during a coding interview. In another article, I will teach you some other features of the Kotlin language that can help you speed up your day-to-day development and your coding interview session. Without any further ado, let’s get into it!

Mutable and Immutable Lists

In Kotlin, most collections have Immutable and Mutable versions. By default, some collections are Immutable, such as List. For instance, when you create a “List” that List will be Immutable, so you can’t add/remove or modify it after its instantiation. To create a list that allows you to change it, you should specify it as Mutable when you create it. Let me show you what I mean in the example below.

Mutable and Immutable HashSets

In this example, I’m gonna show you how you can create a Mutable HashSet in 2 ways, and how to create an Immutable HashSet also.

Priority Queue

Maybe we may not need a priority queue in most scenarios, but I thought it would be helpful to include it here as some coding challenges may require a Minimum Heap implementation which a Priority Queue can come in handy! In the example below, I passed a simple compareBy lambda to the PriorityQueue to sort the elements by length. If you don’t specify a compareBy lambda, then PriorityQueue will fall back into its sorting algorithm, which is an alphabetical sorting.

Mutable and Immutable HashMaps

If we want to talk about the King of Data Structures, I would say we all agree that the HashMap is one of the most powerful and heavily used data structures out there. Let’s see how to create a mutable and immutable HashMap in Kotlin.

Queues

Technically a Queue is a LinkedList with Queue specific functions such as: Poll, Peek, and Remove. There is other implementations of Queue in Kotlin such as ArrayDeque which is faster than LinkedList but for inserting new elements into the queue, an ArrayDeque will take an amortized time whereas a LinkedList takes a constant time. You can choose either one based on your scenario. Here is an example of a LinkedList Queue.

Stacks

Most recursive problems use Stacks as a way of keeping track of the visited nodes. In Kotlin, we can use an ArrayDeque to implement a Stack. Here is an example:

Summary

I hope you found this short article helpful while getting into Kotlin’s data structures! In another Article, I will discuss a few coding challenges and demonstrate how to pick the right data structure for the job.

Wanna know how to solve Google, Meta, and Amazon coding challenges with Kotlin? Make sure you subscribe to get notified when I post new articles for Software Engineering Interviews!

--

--

Alex Manhooei

Staff Software Engineer @ Google. All of my blog posts are my personal opinions and not related to my work at google in any shape or form.