Data structures in interviews
As a beginner in programming, you may be able to work on your projects confidently. However, proving your worth in an interview by showcasing impeccable programming skills may be challenging.
Apart from the pressure that you must feel when your employment depends on a 45-minute talk, there’s one more thing that makes beginner programmers uneasy. You can’t look for answers like you’d typically do if you’re at loss when solving a programming problem.
While you can think your way through a difficult code implementation question, when it comes to data structures, the only thing that’s going to save you is knowledge. Here are the 6 most popular data structures that will help you ace your next coding interview.
An array is one of the most basic data structures. Heaps, linked lists, and others are formed based on arrays. Hence, knowing everything you can about arrays is crucial to let your employers know you’re good at data management.
Most interviews would start by asking basic questions. You may need to explain how arrays work and how implementing arrays would work in different languages. You may also need to provide a couple of examples of languages with zero and 1-based indexing. Most popular languages today are zero-based, while some like Cobol and Fortran are 1-based.
Now, when you’re done with the basic questions, you’ll have to answer something more advanced. Typically, you’ll need to provide an answer to a practical problem and write some code to execute your solution.
A good example of this would be finding the second largest number in the array or deleting duplicate entries.
Apart from the duplicate entry questions, there’s another one that often appears on data structure interviews. This type of question heavily relies on maths, like finding the longest consecutive sequence of numbers in an array or a subarray with the largest sum. You’ll need to work on your math skills to answer any of these.
Stacks and queues
Both stack and queue are linear data structures, but the major difference between them is that stack uses the Last In, First Out method while queue uses the First In, First Out method. Essentially, a stack is a data structure where new elements are put on top and are normally retrieved from the top of the list, and a queue is a structure where new elements are placed in the bottom and are retrieved from the top as well.
Apart from talking about the implementation of these two data structures in practice, you will have to answer questions about implementing one as the other. That is, the interviewer may ask how you would implement a queue using a stack or vice versa.
Linked lists are the basis for implementing queues and stacks, and are quite crucial for creating graphs. In this structure, elements of the array are interlinked instead of being indexed as in an array. This means you do not need to re-declare memory if an array grows too big as it doesn’t have to be close to each other to work.
This data structure is a great solution when you need to delete or insert items into the list constantly, and you aren’t strained in terms of memory usage. Apart from explaining these differences from the arrays, there’s one question that most interviews that bring up linked lists will mention—the loops.
When you insert or delete an element from the list, you need to rearrange pointers, as there may be a loop in there that breaks the code. Hence, finding and eliminating one is one of the most common linked list questions.
You may also have to find solutions to problems like finding and/or deleting certain nodes of the linked list, flattening and sorting lists, and merging sorting lists. Explaining why merge sort is better than quicksort for linked lists may also appear on the list of questions.
Hash tables use a hashing algorithm to assign keys to index values, making an array effectively a two-column table where you can’t choose the value of the first column but can map it with a function itself. The easiest way to imagine a hash table is to assign an index number from 0 to 25 to all letters of the alphabet and then analyze how many times each letter appears in a certain word.
But that’s an easy example. Let’s say a hash table has to present data on response times of your VPN servers in Australia in each Australian town. With so many values to go through the hash function, you’re definitely going to have values that yield identical keys. That’s called hash collision, and it’s one of the major questions on interviews that deal with data structures.
There’s more than one way of solving this problem, and you need to know at least a couple of them and how they would differ. Separate chaining, for instance, is easier to implement than open addressing, but open addressing doesn’t take up as much memory in the end.
Apart from that, you will have to answer some basic hash table questions like finding missing elements and solve maths-related problems like finding a pair with a given sum. Also, expect to hear a question or two about the perfect hash function.
Probably the biggest set of questions you’ll have to answer when it comes to trees is about typology. While a tree data structure is a rather simple structure with a parent node linking to zero or more child nodes, there are so many subtypes that you can spend half an hour just talking about them.
While there are plenty of tree-like structures, you will be mostly talking about binary trees, BSTs, N-ary trees, AVL trees, as well as some other self-balancing trees and Heap structures.
After you’re done explaining the differences between these types of data structures, you’ll be mostly down to questions that deal with either navigating trees or implementing them in real-life situations.
Examples of the first type of questions would be calculating the height of a tree, transforming binary trees to perfect binary trees, or truncating a given tree to remove that lie on a certain path.
A graph is a data structure where a set of nodes is connected with edges. As simple as this sounds, graphs are used everywhere, from GPS-based applications to Facebook. As graphs have a multi-faceted use potential, you may encounter a lot of questions about this data structure during an interview.
One of the easiest questions about graphs you can encounter is detecting and dealing with cycles. Another one deals with the minimum number of steps needed to perform a transformation or an operation.
A huge deal of graph questions is going to be about navigating the network of nodes and edges. You’ll need to explain what topological sorting is to your interviewer and find solutions to problems like finding the shortest path from one node to another in a given graph. You may also need to find the longest path in a DAG, clone a DAG, or calculate the maximum number of edges you can add to one for it to remain acyclic.
Some of the harder problems you may encounter during the interview include the traveling salesman problem, the vertex cover problem, or problems related to the Erdos Renyl model or clustering coefficient.
However, these are higher-tier questions and you may not need to ace them to pass an interview as a beginner in programming.
Excel at your next interview
Learning every possible question about data structures for the interview may be frustrating if you’re just cramming the information. If you want to succeed at interviews consistently, you need to practice and improve your data structure skills.
Work on pet projects to not just learn the typology of data structures but understand how they are used and what are the benefits of one or another structure. If that’s not an option for you, find common data science problems that you can solve and practice that way.
However, that’s going to prepare you for working with data. The only way you can prepare yourself for a data structure interview is by going through many interviews. Take part in mock interviews to polish your skills and excel at the next real interview you schedule.