Data Structures & Algorithms Interview Mock sample questions with answers
10 questions from the Data Structures & Algorithms Interview Mock practice bank, spread across its domains. Pick your answer, then open the explanation to see why each option is right or wrong.
- Question 1Data Structures
A palindrome check for a singly linked list in O(1) extra space finds the middle, reverses the second half in place and compares the halves. Which two statements are true? (Choose two.)
Choose 2.
- A
The caller's list is modified unless the second half is reversed back afterwards
- B
For odd lengths, the middle node can be skipped because it matches itself
- C
Copying the values into a Python list is also O(1) extra space
- D
Recursion would make the check O(1) space as well
- E
The comparison must start from both the head and the original tail at once
Show the answer and explanation
Answer: A and B
Reversing the second half gives a forward pointer into it, so both halves can be compared front to front in O(n) time and O(1) space. In an interview, mention that the input is mutated and restore it (reverse the second half again) if callers might reuse the list.
Why the other options are wrong
C. A list of n values uses O(n) memory.
D. Each recursive call adds a stack frame, so recursion uses O(n) space.
E. A singly linked list cannot walk backwards from the tail; that is why the second half is reversed.
- A
- Question 2Algorithm Design
Meetings run
[[0, 30], [5, 10], [15, 20]]. What is the minimum number of meeting rooms needed?- A
2
- B
3
- C
1
- D
4
Show the answer and explanation
Answer: A
Sort by start and keep a min-heap of end times: [0,30] takes room 1; [5,10] needs room 2; [15,20] starts after 10, so it reuses room 2. The answer equals the maximum number of meetings overlapping at one instant.
Why the other options are wrong
B. [5, 10] ends before [15, 20] starts, so they reuse one room.
C. [0, 30] overlaps the other two, so one room is not enough.
D. There are only three meetings, so four rooms can't be the minimum.
- A
- Question 3Graphs, Sorting and Searching
You must sort the ages (0–120) of 10 million users. Which approach is asymptotically fastest?
- A
Merge sort, because O(n log n) is optimal for sorting
- B
Counting sort, O(n + k) with k = 121 possible values
- C
Quicksort, because it is fastest in practice on large arrays
- D
Binary insertion sort, because binary search makes each insert O(log n)
Show the answer and explanation
Answer: B
The Ω(n log n) lower bound applies to comparison sorts. With integer keys in a small range (k = 121), counting sort counts occurrences and rebuilds the output in O(n + k), which is effectively O(n) for 10 million items.
Why the other options are wrong
A. n log n is optimal only for comparison sorts; small integer keys allow linear time.
C. It still needs about n log n comparisons, while counting sort needs none.
D. Finding the slot is O(log n), but shifting elements makes each insert O(n).
- A
- Question 4Foundations
Solve T(n) = T(n − 1) + n, with T(1) = 1.
- A
Θ(n²)
- B
Θ(n log n)
- C
Θ(2ⁿ)
- D
Θ(n)
Show the answer and explanation
Answer: A
Unrolling: T(n) = n + (n − 1) + … + 2 + T(1) = n(n + 1)/2 = Θ(n²). This is the recurrence of selection sort, and of quicksort's worst case, where every partition peels off only one element.
Why the other options are wrong
B. The problem shrinks by 1 each call, not by half.
C. There's one recursive call per level, not two.
D. Each level does linear work, and there are n levels.
- A
- Question 5Data Structures
You need the k-th largest of n numbers. Which statements comparing quickselect with a size-k min-heap are true? (Choose two.)
Choose 2.
- A
Quickselect averages O(n) but can degrade to O(n²) with bad pivots
- B
The heap approach suits a stream of unknown length that won't fit in memory
- C
The heap approach has a worse worst case than quickselect
- D
Quickselect leaves the input array unchanged
- E
Both approaches need O(n) extra memory
Show the answer and explanation
Answer: A and B
Quickselect partitions around a pivot and recurses into one side: O(n) on average, O(n²) worst case, and median-of-medians guarantees O(n) at a large constant. It needs the whole array in memory and reorders it. A size-k min-heap costs O(n log k) in every case, O(k) memory, and works on streams. When k is small or data streams in, the heap is often the practical choice.
Why the other options are wrong
C. The heap is O(n log k) even in the worst case, which beats quickselect's O(n²) worst case.
D. Quickselect partitions in place, reordering the array, unless you copy it first.
E. The heap needs only O(k) extra; in-place quickselect needs O(1) beyond its recursion.
- A
- Question 6Algorithm Design
In
nums = [2, 3, 0, 1, 4], each value is the maximum jump length from that index. What is the minimum number of jumps from index 0 to the last index?- A
2
- B
3
- C
1
- D
It can't reach the end
Show the answer and explanation
Answer: A
Jump from 0 to 1 (one step), then from 1 jump 3 to index 4: two jumps. The BFS-style greedy expands the reachable window level by level; landing on index 2 (value 0) would be a dead end.
Why the other options are wrong
B. The best route skips index 2 (value 0) entirely.
C. Index 0 can reach at most index 2.
D. Index 1 jumps 3 straight to index 4.
- A
- Question 7Graphs, Sorting and Searching
Every edge in a graph weighs either 0 or 1. Which shortest-path approach is asymptotically fastest and still correct?
- A
0-1 BFS with a deque, O(V + E)
- B
Dijkstra with a binary heap, O((V + E) log V)
- C
Plain BFS that ignores the weights, O(V + E)
- D
Bellman–Ford, O(V·E)
Show the answer and explanation
Answer: A
With weights in {0, 1}, a deque can maintain the frontier in non-decreasing distance order: a 0-edge keeps the same distance, so it goes to the front, and a 1-edge goes to the back. Each vertex settles in O(1) amortised time, for O(V + E) overall.
Why the other options are wrong
B. Correct, but the log factor is unnecessary when weights are only 0 or 1.
C. It treats 0-cost edges as costing 1, so it gives wrong distances.
D. Correct but far slower than needed.
- A
- Question 8Foundations
What does this print?
def max_window(nums, k): s = sum(nums[:k]) best = s for i in range(k, len(nums)): s += nums[i] - nums[i - k] best = max(best, s) return best print(max_window([2, 1, 5, 1, 3, 2], 3))- A
8 - B
9 - C
7 - D
14
Show the answer and explanation
Answer: B
A fixed-size sliding window adds the element entering and subtracts the one leaving, so each step is O(1). The four windows sum to 8, 7, 9 and 6, so the answer is 9. Recomputing each window from scratch would cost O(n·k).
Why the other options are wrong
A. That is 2 + 1 + 5, the first window, but a later window is larger.
C. That is 1 + 5 + 1, the second window, not the best.
D. That is the sum of the whole array, not a window of size 3.
- A
- Question 9Data Structures
What does
depth(root)return for this tree?8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13def depth(node): if node is None: return 0 return 1 + max(depth(node.left), depth(node.right))- A
3, the height measured in edges - B
4 - C
9, the total number of nodes in the tree - D
5, counting the empty child below each leaf
Show the answer and explanation
Answer: B
This definition counts nodes on the longest root-to-leaf path. The path 8 → 3 → 6 → 4 has four nodes, so the result is 4, which is a height of 3 in edges. Interview problems such as LeetCode's "maximum depth" use this node-counting convention, so state which one you mean.
Why the other options are wrong
A. This function returns 0 for an empty tree and 1 for a single node, so it counts nodes.
C. Counting nodes would add the two subtree results instead of taking their maximum.
D. The None check returns 0, so empty children add nothing to the count.
- A
- Question 10Algorithm Design
What does this print?
def can_jump(nums): far = 0 for i, x in enumerate(nums): if i > far: return False far = max(far, i + x) return True print(can_jump([3, 2, 1, 0, 4]))- A
False
- B
True
- C
IndexError
- D
3
Show the answer and explanation
Answer: A
fartracks the furthest reachable index. Indices 0–3 all reach at most index 3, and index 3 holds 0, so at i = 4 the checki > far(4 > 3) returns False. The greedy is O(n) because reachability only needs the maximum reach so far.Why the other options are wrong
B. Index 4 needs a jump from index 3 or earlier with enough range; none has it.
C. The loop only reads valid indices, so nothing goes out of range.
D. The function returns a boolean, not the farthest reachable index.
- A
Practise all 450 DSA questions
Start with the free 15-question diagnostic. It shows where to focus, and your results carry over if you sign up.