Data Structures and Algorithms Interview Questions (DSA MCQs)
Core data structures and algorithms questions asked by product and service companies, with a one-line explanation for each answer.
Q1. Which data structure follows Last In, First Out (LIFO)?
- A. Heap
- B. Queue
- C. Stack
- D. Linked list
Show answerHide answer
Answer: C. Stack
The last item pushed onto a stack is the first one popped.Q2. Which data structure follows First In, First Out (FIFO)?
- A. Tree
- B. Graph
- C. Stack
- D. Queue
Show answerHide answer
Answer: D. Queue
A queue serves items in the order they arrive.Q3. What is the time complexity of binary search on a sorted array?
- A. O(log n)
- B. O(n log n)
- C. O(1)
- D. O(n)
Show answerHide answer
Answer: A. O(log n)
Each step halves the search range.Q4. What is the worst-case time complexity of quicksort?
- A. O(log n)
- B. O(n)
- C. O(n²)
- D. O(n log n)
Show answerHide answer
Answer: C. O(n²)
With bad pivots (e.g. already sorted input and first-element pivot) it degrades to O(n²). Average is O(n log n).Q5. Which sorting algorithm is guaranteed O(n log n) in the worst case?
- A. Insertion sort
- B. Quick sort
- C. Merge sort
- D. Bubble sort
Show answerHide answer
Answer: C. Merge sort
Merge sort always splits in half and merges in linear time.Q6. Which traversal of a binary search tree gives the values in sorted order?
- A. In-order
- B. Post-order
- C. Pre-order
- D. Level-order
Show answerHide answer
Answer: A. In-order
In-order visits left subtree, node, right subtree.Q7. What is the average time to look up a key in a hash table?
- A. O(1)
- B. O(n²)
- C. O(log n)
- D. O(n)
Show answerHide answer
Answer: A. O(1)
Hashing jumps straight to the bucket; collisions make the worst case O(n).Q8. Which algorithm finds the shortest path in a graph with non-negative edge weights?
- A. Dijkstra's algorithm
- B. Kruskal's algorithm
- C. Bubble sort
- D. Depth-first search
Show answerHide answer
Answer: A. Dijkstra's algorithm
Dijkstra greedily expands the closest unvisited vertex. Kruskal finds a minimum spanning tree, not shortest paths.Q9. Breadth-first search (BFS) typically uses which data structure?
- A. Stack
- B. Heap
- C. Hash set only
- D. Queue
Show answerHide answer
Answer: D. Queue
BFS explores level by level, processing vertices in the order they were discovered.Q10. Depth-first search (DFS) can be implemented with recursion or which data structure?
- A. Array of size 1
- B. Queue
- C. Stack
- D. Priority queue
Show answerHide answer
Answer: C. Stack
Recursion uses the call stack; an explicit stack works the same way.Q11. What is the maximum number of nodes in a binary tree of height h (root at height 0)?
- A. 2^h
- B. 2^(h+1) − 1
- C. h²
- D. 2h + 1
Show answerHide answer
Answer: B. 2^(h+1) − 1
Each level doubles: 1 + 2 + 4 + … + 2^h = 2^(h+1) − 1.Q12. Inserting at the beginning of a singly linked list takes:
- A. O(n)
- B. O(log n)
- C. O(1)
- D. O(n²)
Show answerHide answer
Answer: C. O(1)
Just point the new node at the old head.Q13. Accessing the i-th element of an array takes:
- A. O(i²)
- B. O(log n)
- C. O(n)
- D. O(1)
Show answerHide answer
Answer: D. O(1)
Arrays use contiguous memory, so the address is computed directly.Q14. Which data structure is best for implementing a priority queue?
- A. Linked list
- B. Stack
- C. Heap
- D. Array sorted after every insert
Show answerHide answer
Answer: C. Heap
A binary heap gives O(log n) insert and extract-min/max.Q15. What does dynamic programming rely on?
- A. Random choices
- B. Greedy choices only
- C. Sorting the input first
- D. Overlapping subproblems and optimal substructure
Show answerHide answer
Answer: D. Overlapping subproblems and optimal substructure
DP stores answers to repeated subproblems instead of recomputing them.Q16. Which technique is used in the 'two-sum on a sorted array' O(n) solution?
- A. Bit manipulation
- B. Backtracking
- C. Divide and conquer
- D. Two pointers
Show answerHide answer
Answer: D. Two pointers
Move the left pointer up when the sum is too small and the right pointer down when too big.Q17. What is the space complexity of merge sort on an array?
- A. O(n)
- B. O(log n)
- C. O(1)
- D. O(n²)
Show answerHide answer
Answer: A. O(n)
Merging needs a temporary array of size n.Q18. A graph with V vertices stored as an adjacency matrix needs how much space?
- A. O(V²)
- B. O(V + E)
- C. O(V)
- D. O(E)
Show answerHide answer
Answer: A. O(V²)
A V × V matrix. Adjacency lists need O(V + E).Q19. Which problem is typically solved with backtracking?
- A. Computing an average
- B. Binary search
- C. N-Queens
- D. Finding the maximum of an array
Show answerHide answer
Answer: C. N-Queens
Backtracking tries choices and undoes them when they lead to a dead end.Q20. Detecting a cycle in a linked list in O(1) extra space uses:
- A. A hash set
- B. Sorting
- C. Floyd's slow and fast pointers
- D. Recursion
Show answerHide answer
Answer: C. Floyd's slow and fast pointers
If there's a cycle, the fast pointer eventually meets the slow one. A hash set works but uses O(n) space.Q21. What is the time complexity of building a heap from n elements (heapify)?
- A. O(n log n)
- B. O(log n)
- C. O(n²)
- D. O(n)
Show answerHide answer
Answer: D. O(n)
Bottom-up heapify is O(n) because most nodes are near the leaves.Q22. Which of these is a stable sorting algorithm?
- A. Quick sort (typical)
- B. Merge sort
- C. Selection sort
- D. Heap sort
Show answerHide answer
Answer: B. Merge sort
Stable sorts keep equal elements in their original order.Q23. A balanced binary search tree (like AVL) guarantees search in:
- A. O(n)
- B. O(log n)
- C. O(n log n)
- D. O(1)
Show answerHide answer
Answer: B. O(log n)
Balancing keeps the height about log n.Q24. Which data structure is used to check balanced parentheses?
- A. Graph
- B. Stack
- C. Queue
- D. Heap
Show answerHide answer
Answer: B. Stack
Push openers, pop and compare on closers.Q25. Kadane's algorithm solves which problem?
- A. Shortest path
- B. Sorting
- C. Maximum subarray sum
- D. Minimum spanning tree
Show answerHide answer
Answer: C. Maximum subarray sum
It tracks the best sum ending at each position in O(n).Q26. What is a 'trie' mainly used for?
- A. Shortest paths
- B. Prefix searches on strings
- C. Sorting numbers
- D. Matrix multiplication
Show answerHide answer
Answer: B. Prefix searches on strings
Tries store strings character by character, making prefix lookups fast (autocomplete).Q27. Topological sort applies to which kind of graph?
- A. Graphs with cycles
- B. Complete graphs only
- C. Any undirected graph
- D. Directed acyclic graph (DAG)
Show answerHide answer
Answer: D. Directed acyclic graph (DAG)
It orders tasks so every dependency comes before the task that needs it.Q28. Which algorithm builds a minimum spanning tree?
- A. Dijkstra's algorithm
- B. Kruskal's algorithm
- C. KMP
- D. Binary search
Show answerHide answer
Answer: B. Kruskal's algorithm
Kruskal (and Prim) find the cheapest set of edges connecting all vertices.Q29. The 'sliding window' technique is most useful for:
- A. Graph colouring
- B. Tree traversal
- C. Problems on contiguous subarrays or substrings
- D. Sorting linked lists
Show answerHide answer
Answer: C. Problems on contiguous subarrays or substrings
Expand and shrink a window instead of re-checking every subarray.Q30. What is the time complexity of inserting into a Python list at index 0?
- A. O(log n)
- B. O(n)
- C. O(1)
- D. O(n²)
Show answerHide answer
Answer: B. O(n)
Every existing element shifts right. Use collections.deque for O(1) appends on both ends.Q31. Memoization means:
- A. Sorting results
- B. Writing code comments
- C. Caching function results for inputs already computed
- D. Using less memory
Show answerHide answer
Answer: C. Caching function results for inputs already computed
It turns exponential recursion like naive Fibonacci into linear time.Q32. In a min-heap, the smallest element is always:
- A. Anywhere
- B. At the last leaf
- C. At the root
- D. In the middle
Show answerHide answer
Answer: C. At the root
Every parent is ≤ its children, so the minimum sits at the top.Q33. How many edges does a tree with n nodes have?
- A. 2n
- B. n − 1
- C. n + 1
- D. n
Show answerHide answer
Answer: B. n − 1
A tree is connected with no cycles, which needs exactly n − 1 edges.Q34. Which search works on unsorted data?
- A. Linear search
- B. Exponential search
- C. Binary search
- D. Interpolation search
Show answerHide answer
Answer: A. Linear search
Without order, you have to check elements one by one.Q35. A recursive function without a base case will cause:
- A. Faster execution
- B. A syntax error
- C. Stack overflow (RecursionError in Python)
- D. It to return None
Show answerHide answer
Answer: C. Stack overflow (RecursionError in Python)
Each call adds a stack frame until memory runs out.
Practised these? Now prove it.
Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.
Start a mock test