Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
Recursive quicksort can need O(n) stack depth on adversarial input. Which change GUARANTEES O(log n) extra stack space even in the worst case?
- A. Pick the pivot uniformly at random
- B. Recurse only into the smaller partition and handle the larger one with a loop (tail-call elimination)
- C. Use median-of-three pivot selection
- D. Use three-way (Dutch flag) partitioning
Correct answer: B. Recurse only into the smaller partition and handle the larger one with a loop (tail-call elimination)
Each recursive call then gets at most half the current range, so depth is at most log₂ n regardless of pivot quality. Random pivots give O(log n) only in expectation, median-of-three can still be beaten by crafted input, and 3-way partitioning helps only with many duplicates.
More Coding Interview Problems questions
- Problem: Find the shortest substring of s that contains every character of t, counting duplicates (return the leftmost one on ties)…
- Problem: Count subarrays with EXACTLY k distinct values using exactly(k) = atMost(k) − atMost(k − 1). python def atmost(a, k): cnt, l, res…
- Find the bug: the code below should return the length of the longest substring without repeating characters. python def longest(s): last…
- Problem: Count the UNIQUE triplets (as value sets) that sum to zero, using sort + two pointers with duplicate skipping. python def…
- Problem: Count subarrays whose sum is exactly k when values may be negative (prefix sums + hash map of prefix counts). python def count(a…
- Search for a value in a sorted array that was rotated and MAY CONTAIN DUPLICATES (e.g. [1, 1, 1, 1, 0, 1, 1]). What is the worst-case time…