Johannes Baum
May 1 · 4 min read

Data structures have become essential when it comes to coding interviews nowadays. Large tech companies like Google, Microsoft, Facebook, Apple and Amazon started to focus a lot on algorithms and data structures in their interviews and many smaller companies followed this trend. These interviews typically involve coding on a whiteboard using a common programming language like C++, Java or Python.

During the last years JavaScript gained a lot of attention and is getting more relevant for interviews. However, most books and coding interview tutorials focus on C++, Java or Python. It can be tricky to convert ideas and data structures from those languages to performant JavaScript. This article focuses on the most useful interview data structures and how to use them in JavaScript. There will also be an equivalent piece of Java code that shows the application of the data structure in Java.

Make sure you are familiar with Big O notation to understand the asymptotic time complexity of the different operations on the mentioned data structures.

The following operations will be stated for the data structures:

  • Insertion: insert an element
  • Random access: access any element
  • Find: find a specific element
  • Random removal: remove any element

Please note that these operations are only a selection and there exist many more relevant operations for most data structures.

Array

  • Insertion: O(1)
  • Random Access: O(1)
  • Find: O(n)
  • Random Removal: –

Arrays are a classic data structure and natively available in JavaScript. In many programming languages like Java or C/C++ arrays have a fixed size but allow insertion and random access in constant time. JavaScript gives you dynamic arrays by default and you don’t need to pass a size on creation.

Java

String[] myArray = new String[2];
myArray[0] = "one entry";
myArray[1] = "another entry";
System.out.println(myArray[0]); // "one entry"
System.out.println(myArray[1]); // "another entry"

JavaScript

const myArray = [];
myArray[0] = 'one entry';
myArray[1] = 'another entry';
console.log(myArray[0]); // "one entry"
console.log(myArray[1]); // "another entry"

Array List / Vector

  • Insertion: O(1)*
  • Random Access: O(1)
  • Find: O(n)
  • Random removal: O(n)

*Actually the worst case is O(n), but one can proof that for every insert that performs that bad, there will be n inserts that perform in O(1). So the worst case is not very useful here. For more details read this article: Amortized Time Complexity of Algorithms

Array lists are heavily used in Java whenever a dynamic sized array is needed. So if you don’t know the amount of elements to store in advance it is hard to find the right size for an array. Therefore array lists and vectors automatically grow as you insert new elements. As mentioned before JavaScript gives you dynamic arrays by default so you can just use plain JavaScript arrays for this purpose.

Java

ArrayList<String> myArrayList = new ArrayList<>();
myArrayList.add("one entry");
myArrayList.add("another entry");
System.out.println(myArrayList.size()); // "2"
System.out.println(myArrayList.get(1)); // "another entry"
myArrayList.remove(0);System.out.println(myArrayList.size()); // "1"

JavaScript

const myArrayList = [];
myArrayList.push('one entry');
myArrayList.push('another entry');
console.log(myArrayList.length); // "2"
console.log(myArrayList[1]); // "another entry"
// delete one element at zero based index 1
myArrayList.splice(1, 1);
console.log(myArrayList.length); // "1"

Hash Map

  • Insertion: O(1)
  • Random access: O(1)
  • Find: O(1)
  • Random removal: O(1)

Hash maps are one of the most useful data structures since they allow you all of the specified operations in constant time. JavaScript was lacking a good implementation for a long time and Objects have been used for this purpose a lot in the past. However, if you want to have a hash map that has all the advantages of a Java HashMap then you should use Map which was added with ES6.

Java

HashMap<String, Integer> myHashMap = new HashMap<>();
myHashMap.put("six", 6);
myHashMap.put("nine", 9);
System.out.println(myHashMap.get("nine")); // "9"
System.out.println(myHashMap.containsKey("six")); // "true"
System.out.println(myHashMap.size()); // "2"
myHashMap.remove("six");System.out.println(myHashMap.size()); // "1"

JavaScript

const myHashMap = new Map();
myHashMap.set("six", 6);
myHashMap.set("nine", 9);
console.log(myHashMap.get("nine")); // "9"
console.log(myHashMap.has("six")); // "true"
console.log(myHashMap.size); // "2"
myHashMap.delete("six");console.log(myHashMap.size); // "1"

Hash Set

  • Insertion: O(1)
  • Random access: -
  • Find: O(1)
  • Random removal: O(1)

A hash set is a useful data structure when you only need to add/remove elements and check if an element is already contained in the hash set or not. You could also do this with a hash map that maps from the desired object to a Boolean. However, the usage of hash set is more concise. Note that random access does not really make sense for a hash set, because you already have the object at hand that you want to fetch from it.

Java

HashSet<String> myHashSet = new HashSet<>();
myHashSet.add("Elixir");
myHashSet.add("Typescript");
System.out.println(myHashSet.contains("Elixir")); // "true"
System.out.println(myHashSet.size()); // "2"
myHashSet.remove("Elixir"); System.out.println(myHashSet.contains("Elixir")); // "false"

JavaScript

const myHashSet = new Set();
myHashSet.add("Elixir");
myHashSet.add("Typescript");
console.log(myHashSet.has("Elixir")); // "true"
console.log(myHashSet.size); // "2"
myHashSet.delete("Elixir");console.log(myHashSet.has("Elixir")); // "false"

I hope you enjoyed this quick overview about the JavaScript implementation of the most useful data structures for coding interviews. Also check out my follow up stories:

I wish you good luck for your interviews!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade