Mahima Phalkey
Data Science Consultant at almaBetter
This article contains a comprehensive list of 100 commonly asked data structure interview questions, to help you prepare for your next interview
Welcome to the arena of data structures! As a computer science student or a programmer, expertise in data structures is vital for writing efficient algorithms and fixing complicated problems.
In this article, we've compiled a listing of 100 data structure interview questions and answers, so as to assist you prepare for technical interviews. The data structure questions and aswers are designed to cover a broad variety of data structures, which includes arrays, linked lists, stacks, queues, trees, graphs, hash tables, and heaps.
The questions range in difficulty from newbie to advanced, and cover topics such as time and space complexity, algorithms, data modeling, and design patterns. Whether you're a newbie or a skilled programmer, this article is an excellent resource to test your expertise and enhance your understanding of data structures.
We hope that this article will assist you on your journey to becoming a better programmer and ace your next technical interview!
Let’s begin with some basic DSA questions and answers. We will also include DSA Coding questions with theoretical concepts.
1. What is data structure?
Answer: Data structure refers to the way data is organized and manipulated. It seeks to find ways to make data access more efficient. When dealing with the data structure, we not only focus on one piece of data but the different set of data and how they can relate to one another in an organized manner.
2. Differentiate between file and structure storage structure.
Answer: The key difference between both the data structure is the memory area that is being accessed. When dealing with the structure that resides the main memory of the computer system, this is referred to as storage structure. When dealing with an auxiliary structure, we refer to it as file structures.
3. When is a binary search best applied?
Answer: A binary search is an algorithm that is best applied to search a list when the elements are already in order or sorted. The list is searched starting in the middle, such that if that middle value is not the target search key, it will check to see if it will continue the search on the lower half of the list or the higher half. The split and search will then continue in the same manner.
4. What is a linked list?
Answer: A linked list is a sequence of nodes in which each node is connected to the node following it. This forms a chain-like link for data storage.
5. How do you reference all the elements in a one-dimension array?
Answer: To reference all the elements in a one -dimension array, you need to use an indexed loop, So that, the counter runs from 0 to the array size minus one.
6. In what areas do data structures are applied?
Answer: Data structures are essential in almost every aspect where data is involved. In general, algorithms that involve efficient data structure is applied in the following areas: numerical analysis, operating system, A.I., compiler design, database management, graphics, and statistical analysis, to name a few.
7. What is LIFO?
Answer: LIFO is a short form of Last In First Out. It refers how data is accessed, stored and retrieved. Using this scheme, data that was stored last should be the one to be extracted first. This also means that in order to gain access to the first data, all the other data that was stored before this first data must first be retrieved and extracted.
8. What is a queue?
Answer: A queue is a data structure that can simulate a list or stream of data. In this structure, new elements are inserted at one end, and existing elements are removed from the other end.
Implement Queue using Linked List
Loading...
9. What are binary trees?
Answer: A binary tree is one type of data structure that has two nodes, a left node, and a right node. In programming, binary trees are an extension of the linked list structures.
10. Which data structures are applied when dealing with a recursive function?
Answer: Recursion, is a function that calls itself based on a terminating condition, makes use of the stack. Using LIFO, a call to a recursive function saves the return address so that it knows how to return to the calling function after the call terminates.
11. What is a stack?
Answer: A stack is a data structure in which only the top element can be accessed. As data is stored in the stack, each data is pushed downward, leaving the most recently added data on top.
12. Explain Binary Search Tree with it’s implementation
Answer: A binary search tree stores data in such a way that they can be retrieved very efficiently. The left subtree contains nodes whose keys are less than the node's key value, while the right subtree contains nodes whose keys are greater than or equal to the node's key value. Moreover, both subtrees are also binary search trees.
Loading...
13. What are multidimensional arrays?
Answer: Multidimensional arrays make use of multiple indexes to store data. It is useful when storing data that cannot be represented using single-dimensional indexing, such as data representation in a board game, tables with data stored in more than one column.
14. Are linked lists considered linear or non-linear data structures?
Answer: It depends on where you intend to apply linked lists. If you based it on storage, a linked list is considered non-linear. On the other hand, if you based it on access strategies, then a linked list is considered linear.
Also Read: Difference Between Linear Search and Binary Search
15. How does dynamic memory allocation help in managing data?
Answer: Apart from being able to store simple structured data types, dynamic memory allocation can combine separately allocated structured blocks to form composite structures that expand and contract as needed.
16. What is FIFO?
Answer: FIFO stands for First-in, First-out, and is used to represent how data is accessed in a queue. Data has been inserted into the queue list the longest is the one that is removed first.
17. What is an ordered list?
Answer: An ordered list is a list in which each node's position in the list is determined by the value of its key component, so that the key values form an increasing sequence, as the list is traversed.
18. What is merge sort?
Answer: Merge sort, is a divide-and-conquer approach for sorting the data. In a sequence of data, adjacent ones are merged and sorted to create bigger sorted lists. These sorted lists are then merged again to form an even bigger sorted list, which continues until you have one single sorted list.
19. Differentiate NULL and VOID
Answer: Null is a value, whereas Void is a data type identifier. A variable that is given a Null value indicates an empty value. The void is used to identify pointers as having no initial size.
20. What is the primary advantage of a linked list?
Answer: A linked list is an ideal data structure because it can be modified easily. This means that editing a linked list works regardless of how many elements are in the list.
21. What is the difference between a PUSH and a POP?
Answer: Pushing and popping applies to the way data is stored and retrieved in a stack. A push denotes data being added to it, meaning data is being “pushed” into the stack. On the other hand, a pop denotes data retrieval, and in particular, refers to the topmost data being accessed.
22. What is a linear search?
Answer: A linear search refers to the way a target key is being searched in a sequential data structure. In this method, each element in the list is checked and compared against the target key. The process is repeated until found or if the end of the file has been reached.
23. How does variable declaration affect memory allocation?
Answer: The amount of memory to be allocated or reserved would depend on the data type of the variable being declared. For example, if a variable is declared to be of integer type, then 32 bits of memory storage will be reserved for that variable.
24. What is the advantage of the heap over a stack?
Answer: The heap is more flexible than the stack. That's because memory space for the heap can be dynamically allocated and de-allocated as needed. However, the memory of the heap can at times be slower when compared to that stack.
25. What is a postfix expression?
Answer: A postfix expression is an expression in which each operator follows its operands. The advantage of this form is that there is no need to group sub-expressions in parentheses or to consider operator precedence.
26. What is Data abstraction?
Answer: Data abstraction is a powerful tool for breaking down complex data problems into manageable chunks. This is applied by initially specifying the data objects involved and the operations to be performed on these data objects without being overly concerned with how the data objects will be represented and stored in memory.
27. How do you insert a new item in a binary search tree?
Answer: Assuming that the data to be inserted is a unique value (that is, not an existing entry in the tree), check first if the tree is empty. If it's empty, just insert the new item in the root node. If it's not empty, refer to the new item's key. If it's smaller than the root's key, insert it into the root's left subtree, otherwise, insert it into the root's right subtree.
28. How does a selection sort work for an array?
Answer: The selection sort is a fairly intuitive sorting algorithm, though not necessarily efficient. In this process, the smallest element is first located and switched with the element at subscript zero, thereby placing the smallest element in the first position. The smallest element remaining in the subarray is then located next to subscripts 1 through n-1 and switched with the element at subscript 1, thereby placing the second smallest element in the second position. The steps are repeated in the same manner till the last element.
29. How do signed and unsigned numbers affect memory?
Answer: In the case of signed numbers, the first bit is used to indicate whether positive or negative, which leaves you with one bit short. With unsigned numbers, you have all bits available for that number. The effect is best seen in the number range (an unsigned 8-bit number has a range 0-255, while the 8-bit signed number has a range -128 to +127.
30. What is the minimum number of nodes that a binary tree can have?
Answer: A binary tree can have a minimum of zero nodes, which occurs when the nodes have NULL values. Furthermore, a binary tree can also have 1 or 2 nodes.
31. What are dynamic data structures?
Answer: Dynamic data structures are structures that expand and contract as a program runs. It provides a flexible means of manipulating data because it can adjust according to the size of the data.
32. In what data structures are pointers applied?
Answer: Pointers that are used in linked list have various applications in the data structure. Data structures that make use of this concept include the Stack, Queue, Linked List and Binary Tree.
33. Do all declaration statements result in a fixed reservation in memory?
Answer: Most declarations do, with the exemption of pointers. Pointer declaration does not allocate memory for data, but for the address of the pointer variable. Actual memory allocation for the data comes during run-time.
34. What are ARRAYs?
Answer: When dealing with arrays, data is stored and retrieved using an index that refers to the element number in the data sequence. This means that data can be accessed in any order. In programming, an array is declared as a variable having a number of indexed elements.
35. What is the minimum number of queues needed when implementing a priority queue?
Answer: The minimum number of queues needed in this case is two. One queue is intended for sorting priorities while the other queue is used for actual storage of data.
36. Which sorting algorithm is considered the fastest?
Answer: There are many types of sorting algorithms: quick sort, bubble sort, balloon sort, radix sort, Not one can be considered the fastest because each algorithm is designed for a particular data structure and data set. It would depend on the data set that you would want to sort.
37. Differentiate STACK from ARRAY.
Answer: Stack follows a LIFO pattern. It means that data access follows a sequence wherein the last data to be stored when the first one to be extracted. Arrays, on the other hand, does not follow a particular order and instead can be accessed by referring to the indexed element within the array.
38. Give a basic algorithm for searching a binary search tree.
Answer:
1. if the tree is empty, then the target is not in the tree, end search
2. if the tree is not empty, the target is in the tree
3. check if the target is in the root item
4. if a target is not in the root item, check if a target is smaller than the root's value
5. if a target is smaller than the root's value, search the left subtree else, search the right subtree
39. What is a dequeue?
Answer: A dequeue is a double-ended queue. This is a structure wherein elements can be inserted or removed from either end.
40. What is a bubble sort and how do you perform it?
Answer: A bubble sort is one sorting technique that can be applied to data structures such as an array. It works by comparing adjacent elements and exchanges their values if they are out of order. This method lets the smaller values “bubble” to the top of the list, while the larger value sinks to the bottom.
41. What are the parts of a linked list?
Answer: A linked list typically has two parts: the head and the tail. Between the head and tail lie the actual nodes. All these nodes are linked sequentially.
42. How does selection sort work?
Answer: Selection sort works by picking the smallest number from the list and placing it at the front. This process is repeated for the second position towards the end of the list. It is the simplest sort algorithm.
43. What is a graph and implement graph using adjacency list?
Answer: A graph is one type of data structure that contains a set of ordered pairs. These ordered pairs are also referred to as edges or arcs and are used to connect nodes where data can be stored and retrieved.
Loading...
44. Differentiate linear from nonlinear data structure.
Answer: The linear data structure is a structure wherein data elements are adjacent to each other. Examples of linear data structure include arrays, linked lists, stacks, and queues. On the other hand, a non-linear data structure is a structure wherein each data element can connect to more than two adjacent data elements. Examples of nonlinear data structure include trees and graphs.
45. What is an AVL tree?
Answer: An AVL tree is a type of binary search tree that is always in a state of partially balanced. The balance is measured as a difference between the heights of the subtrees from the root. This selfbalancing tree was known to be the first data structure to be designed as such.
46. What are doubly linked lists?
Answer: Doubly linked lists are a special type of linked list wherein traversal across the data elements can be done in both directions. This is made possible by having two links in every node, one that links to the next node and another one that connects to the previous node.
47. What is Huffman's algorithm?
Answer: Huffman's algorithm is used for creating extended binary trees that have minimum weighted path lengths from the given weights. It makes use of a table that contains the frequency of occurrence for each data element.
48. What is Fibonacci search?
Answer: Fibonacci search is a search algorithm that applies to a sorted array. It makes use of a divide and conquer approach that can significantly reduce the time needed in order to reach the target element.
49. Briefly explain recursive algorithm.
Answer: Recursive algorithm targets a problem by dividing it into smaller, manageable sub-problems. The output of one recursion after processing one sub-problem becomes the input to the next recursive process.
50. How do you search for a target key in a linked list?
Answer: To find the target key in a linked list, you have to apply sequential search. Each node is traversed and compared with the target key, and if it is different, then it follows the link to the next node. This traversal continues until either the target key is found or if the last node is reached.
51. What are various data-structures available?
Answer: Data structure availability may vary by programming languages. Commonly available data structures are list, arrays, stack, queues, graph, tree, etc.
52. What is algorithm?
Answer: Algorithm is a step by step procedure, which defines a set of instructions to be executed in certain order to get the desired output.
53. Why we need to do algorithm analysis?
Answer: A problem can be solved in more than one ways. So, many solution algorithms can be derived for a given problem. We analyze available algorithms to find and implement the best suitable algorithm.
54. What are the criteria of algorithm analysis?
Answer: An algorithm are generally analyzed on two factors − time and space. That is, how much execution time and how much extra space required by the algorithm.
55. What is asymptotic analysis of an algorithm?
Answer: Asymptotic analysis of an algorithm, refers to defining the mathematical boundation/framing of its run-time performance. Using asymptotic analysis, we can very well conclude the best case, average case and worst case scenario of an algorithm.
56. What are asymptotic notations?
Answer: Asymptotic analysis can provide three levels of mathematical binding of execution time of an algorithm − Best case is represented by Οn notation. Worst case is represented by Ωn notation. Average case is represented by Θn notation.
57. What is linear data structure?
Answer: A linear data-strucutre has sequentially arranged data items. The next time can be located in the next memory address. It is stored and accessed in a sequential manner. Array and list are example of linear data structure.
58. What are common operations that can be performed on a data-structure?
Answer: The following operations are commonly performed on any data-structure
59. Briefly explain the approaches to develop algorithms.
Answer: There are three commonly used approaches to develop algorithms
-Greedy Approach − finding solution by choosing next best option
-Divide and Conquer − diving the problem to a minimum possible sub-problem and solving them independently
-Dynamic Programming − diving the problem to a minimum possible sub-problem and solving them combinedly
60. Give some examples greedy algorithms.
Answer: The below given problems find their solution using greedy algorithm approach −
-Travelling Salesman Problem
-Prim's Minimal Spanning Tree Algorithm
-Kruskal's Minimal Spanning Tree Algorithm
-Dijkstra's Minimal Spanning Tree Algorithm
-Graph - Map Coloring
-Graph - Vertex Cover
-Knapsack Problem
-Job Scheduling Problem
61. What are some examples of divide and conquer algorithms?
Answer: The below given problems find their solution using divide and conquer algorithm approach −
-Merge Sort
-Quick Sort
-Binary Search
-Strassen's Matrix Multiplication
-Closest pair points
62. What are some examples of dynamic programming algorithms?
Answer: The below given problems find their solution using
-divide and conquer algorithm approach
-Fibonacci number series
-Knapsack problem
-Tower of Hanoi
-All pair shortest path by Floyd-Warshall
-Shortest path by Dijkstra
-Project scheduling
63. What is a linked-list?
Answer: A linked-list is a list of data-items connected with links i.e. pointers or references. Most modern high-level programming language does not provide the feature of directly accessing memory location, therefore, linked-list are not supported in them or available in form of inbuilt functions.
64. What is stack and also write a code to implement stack?
Answer: In data-structure, stack is an Abstract Data Type ADT used to store and retrieve values in Last In First Out method.
Loading...
65. Why do we use stacks?
Answer: Stacks follows LIFO method and addition and retrieval of a data item takes only Οn time. Stacks are used where we need to access data in the reverse order or their arrival. Stacks are used commonly in recursive function calls, expression parsing, depth first traversal of graphs etc.
66. What operations can be performed on stacks?
Answer: The below operations can be performed on a stack −
-push − adds an item to stack
-pop − removes the top stack item
-peek − gives value of top item without removing it
-isempty − checks if stack is empty
-isfull − checks if stack is full
67. What is a queue in data-structure?
Answer: Queue is an abstract data structure, somewhat similar to stack. In contrast to stack, queue is opened at both end. One end is always used to insert data enqueue and the other is used to remove data dequeue. Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
68. Why do we use queues?
Answer: As queues follows FIFO method, they are used when we need to work on data-items in exact sequence of their arrival. Every operating system maintains queues of various processes. Priority queues and breadth first traversal of graphs are some examples of queues.
69. What operations can be performed on Queues?
Answer: The below operations can be performed on a stack −
-enqueue − adds an item to rear of the queue
-dequeue − removes the item from front of the queue
-peek − gives value of front item without removing it
-isempty − checks if stack is empty
-isfull − checks if stack is full
70. What is linear searching?
Answer: Linear search tries to find an item in a sequentially arranged data type. These sequentially arranged data items known as array or list, are accessible in incrementing memory location. Linear search compares expected data item with each of data items in list or array. The average case time complexity of linear search is Οn and worst case complexity is Ο(n 2). Data in target arrays/lists need not to be sorted.
71. What is binary search?
Answer: A binary search works only on sorted lists or arrays. This search selects the middle which splits the entire list into two parts. First the middle is compared. This search first compares the target value to the mid of the list. If it is not found, then it takes decision on whether.
72. What is bubble sort and how bubble sort works?
Answer: Bubble sort is comparison based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order. Because the time complexity is Ο(n 2), it is not suitable for large set of data.
73. Tell me something about 'insertion sort'?
Answer: Insertion sort divides the list into two sub-list, sorted and unsorted. It takes one element at time and finds it appropriate location in sorted sub-list and insert there. The output after insertion is a sorted sub-list. It iteratively works on all the elements of unsorted sub-list and inserts them to sorted sublist in order.
74. What is selection sort?
Answer: Selection sort is in-place sorting technique. It divides the data set into two sub-lists: sorted and unsorted. Then it selects the minimum element from unsorted sub-list and places it into the sorted list. This iterates unless all the elements from unsorted sub-list are consumed into sorted sub-list.
75. How insertion sort and selection sorts are different?
Answer: Both sorting techniques maintains two sub-lists, sorted and unsorted and both take one element at a time and places it into sorted sub-list. Insertion sort works on the current element in hand and places it in the sorted array at appropriate location maintaining the properties of insertion sort. Whereas, selection sort searches the minimum from the unsorted sub-list and replaces it with the current element in hand.
76. What is merge sort and how it works?
Answer: Merge sort is sorting algorithm based on divide and conquer programming approach. It keeps on dividing the list into smaller sub-list until all sub-list has only 1 element. And then it merges them in a sorted way until all sub-lists are consumed. It has run-time complexity of Οnlogn and it needs Οn auxiliary space.
77. What is shell sort?
Answer: Shell sort can be said a variant of insertion sort. Shell sort divides the list into smaller sublist based on some gap variable and then each sub-list is sorted using insertion sort. In best cases, it can perform upto Οnlogn.
78. How quick sort works?
Answer: Quick sort uses divide and conquer approach. It divides the list in smaller 'partitions' using 'pivot'. The values which are smaller than the pivot are arranged in the left partition and greater values are arranged in the right partition. Each partition is recursively sorted using quick sort.
79. How depth first traversal works?
Answer: Depth First Search algorithmDFS traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search when a dead end occurs in any iteration.
80. How breadth first traversal works?
Answer: Breadth First Search algorithmBFS traverses a graph in a breadthwards motion and uses a queue to remember to get the next vertex to start a search when a dead end occurs in any iteration.
81. What is a tree?
Answer: A tree is a minimally connected graph having no loops and circuits.
82. What is a binary tree?
Answer: A binary tree has a special condition that each node can have two children at maximum.
83. What is a binary search tree?
Answer: A binary search tree is a binary tree with a special provision where a node's left child must have value less than its parent's value and node's right child must have value greater than it's parent value.
84. What is tree traversal?
Answer: Tree traversal is a process to visit all the nodes of a tree. Because, all nodes are connected via edges links we always start from the root head node. There are three ways which we use to traverse a tree
-In-order Traversal
-Pre-order Traversal
-Post-order Traversal
See the below image of a binary search tree, and traverse it using all available methods −
85. What is a spanning tree?
Answer: A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible number of edges. A spanning tree does not have cycles and it can not be disconnected.
86. How many spanning trees can a graph has?
Answer: It depends on how connected the graph is. A complete undirected graph can have maximum n n-1 number of spanning trees, where n is number of nodes.
87. How Kruskal's algorithm works?
Answer: This algorithm treats the graph as a forest and every node it as an individual tree. A tree connects to another only and only if it has least cost among all available options and does not violate MST properties.
88. How Prim's algorithm finds spanning tree?
Answer: Prim's algorithm treats the nodes as a single tree and keeps on adding new nodes to the spanning tree from the given graph.
89. What is a minimum spanning tree MST ?
Answer: In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight that all other spanning trees of the same graph.
90. What is a recursive function?
Answer: A recursive function is one which calls itself, directly or calls a function that in turn calls it. Every recursive function follows the recursive properties − base criteria where functions stops calling itself and progressive approach where the functions tries to meet the base criteria in each iteration.
91. What is tower of hanoi?
Answer: Tower of Hanoi, is a mathematical puzzle which consists of three tower pegs and more than one rings. All rings are of different size and stacked upon each other where the large disk is always below the small disk. The aim is to move the tower of disk from one peg to another, without breaking its properties.
92. What is fibonacci series?
Answer: Fibonacci Series generates subsequent number by adding two previous numbers. For example − 0 1 1 2 3 5 8 13.
93. What is hashing?
Answer: Hashing is a technique to convert a range of key values into a range of indexes of an array. By using hash tables, we can create an associative data storage where data index can be find by providing its key values.
94. What is interpolation search technique?
Answer: Interpolation search is an improved variant of binary search. This search algorithm works on the probing position of required value.
95. What is the prefix and post fix notation of a + b * c + d ?
Answer:
Prefix Notation − * + a b + c d
Postfix Notation − a b + c d + *
96. Describe the types of Data Structures?
Answer: The following are the types of data structures:
97. What are some applications of Data Structures?
Answer: Numerical analysis, operating system, AI, compiler design, database management, graphics, statistical analysis, and simulation.
98. Given a large file containing integers, write a data structure that can quickly return the frequency of a given integer. Assume that the file cannot fit into memory.
Answer: One solution to this problem is to use a variation of the Bloom filter called the Count-Min Sketch. The Count-Min Sketch is a probabilistic data structure that can estimate the frequency of an element in a stream of data. It uses multiple hash functions to map each element to a set of counters, which are then incremented each time the element is encountered in the stream. The frequency of the element is estimated by taking the minimum value of the counters corresponding to the hash functions.
99. Design a data structure to efficiently support the following operations:
Answer: One solution to this problem is to use a self-balancing binary search tree like the Red-Black tree or AVL tree. These trees have efficient implementations of the insert and delete operations, as well as an algorithm to find the kth smallest element in O(log n) time, where n is the number of elements in the tree. The algorithm works by maintaining a count of the number of nodes in each subtree and using this information to traverse the tree to the kth smallest element.
Get our essential DSA cheat sheets now and elevate your coding and problem-solving skills!
In conclusion, mastering data structures is important for programmers to put in writing efficient algorithms and solve complicated problems. This article provides a complete listing of 100 interview questions covering a huge variety of data structures, inclusive of arrays, linked lists, stacks, queues, trees, graphs, hash tables, and heaps. These questions are designed to challenge both beginner and advanced programmers and cover diverse topics consisting of time and space complexity, algorithms, data modeling, and design patterns. By the usage of this resource, programmers can test their knowledge and enhance their understanding of data structures, ultimately leading to success in technical interviews and becoming better programmers.
Related Articles
Top Tutorials