Data Structures & AlgorithmsHardGoogle · Flipkart interview
Which statement about selecting the k-th smallest of n unsorted numbers is correct?
- A. Quickselect is O(n log n) expected, the same as sorting
- B. A min-heap of size k gives O(n) worst case for any k
- C. Quickselect is O(n) expected but O(n²) worst case; median-of-medians pivoting makes it O(n) worst case
- D. Median-of-medians is O(n log n) worst case but faster in practice than quickselect
Correct answer: C. Quickselect is O(n) expected but O(n²) worst case; median-of-medians pivoting makes it O(n) worst case
Quickselect recurses into one side only, so expected work is n + n/2 + … = O(n); bad pivots give O(n²). Grouping into 5s and taking the median of medians guarantees a 30–70 split, so T(n) ≤ T(n/5) + T(7n/10) + O(n) = O(n). A size-k heap costs O(n log k).
More Coding Interview Problems questions
- Problem: Find the smallest divisor d such that Σ ceil(a[i] / d) ≤ threshold. python def smallestdiv(a, t): lo, hi = 1, max(a) while lo <…
- Problem: Buses i take time[i] minutes per trip and run independently and repeatedly. Find the minimum time for all buses together to…
- Problem: Use bisect to count how many readings lie in the closed range [2, 7]. python from bisect import bisectleft, bisectright a = [1…
- Problem: Vertical order traversal: group nodes by column (left child c − 1, right child c + 1); inside a column sort by row, then by…
- Problem: Zigzag level-order traversal (left to right, then right to left, alternating). python class T: def init(s, v): s.v, s.l, s.r = v…
- Find the bug: this BST validator compares each node only with its own children. python class T: def init(s, v): s.v, s.l, s.r = v, None…