How to identify which Data Structure to use.

Muskan Agarwal
CodeChef-VIT
Published in
3 min readJan 25, 2022

So I have seen a lot of videos giving you the road map of How to learn Data Structure, 3-month plan, 5-month plan, etc. But I guess we forget the most important part that how can a student identify what to use and how to identify which Data Structure will be the best.

This image is taken from Interview Bit & is basically a classification of Data Structures

I am here to help you but there are some prerequisites before you continue reading. So you should know what Data Structures are and have a fair amount of knowledge of all the data structures like Arrays, Strings, LinkedList, Trees, Graphs, Heap, and obviously Dynamic Programing.

👾Tip 1:

If the array is sorted or there are 2 pointers.

If the array is sorted then the binary search should be the first thing that should come to your mind and there is a high probability that you will get to a solution with that.

Another thing that can help you with the sorted array is a two-pointer keep an i at the start and j at the ending and try if you can solve it by moving those.

👾Tip 2:

If you are given a linked list

Linked List is my favourite Data Structure and after solving 100+ questions on Linked List I have realized that the two-pointer (in which you have a slow pointer that moves one step and a fast pointer that moves two steps) method solves the problem.

Weather you take an example of questions like: loop in a link list, where 2 link lists are meeting, etc

👾Tip 3:

If asked for top/least K items

If you see K you should immediately think of a Heap. If it is a direct question then you can easily figure it out but sometimes the question is in a form of a story like top 3 from 10 then heap can be helpful.

👾Tip 4:

Tree or Graph Question

This was one of the scariest topics for me but I figured that learning BFS & DFS will solve most of the questions. I talked to so many friends who gave interviews in companies like Amazon, Microsoft, BNY Mellon, etc most of them told me that graph question in an interview was easily solved just by using one of the two approaches.

👾Tip 5:

If you have been given frequency/ duplicates

In these cases, hashmaps come handy because you can store key-value pairs at better complexity as compared to storing in an array.

👾Tip 6:

If asked for maximum/ minimum subarray/ subset

In such cases, Dynamic Programming comes handy

👾Tip 7:

If permutations or subsets

Recursion or BackTracking can be helpful in such cases

While the above tips can help you solve 90% of the questions still there is no set method through which you can identify what DS we have to use. There are questions where Binary Search is used even in an unsorted array so it’s about building your logic and choosing the right data structure that solves your question in the least time complexity.

Let me know if you have any suggestions or more tips to add to this blog I’d love to add them. 😋

Video on this: https://www.youtube.com/watch?v=snDuhHSLjB4&t=37s

--

--

Muskan Agarwal
CodeChef-VIT

Frontend Web Developer || Exploring ReactJS || VIT Vellore