Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard
The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 0, 1, 0, 1, 1, 0] (values 0, 1, 2 only) in place. What is the resulting array?
- A. 0, 0, 0, 0, 2, 1, 1, 1
- B. 0, 0, 0, 0, 1, 1, 1, 2
- C. 2, 1, 1, 1, 0, 0, 0, 0
- D. 0, 1, 1, 0, 1, 0, 0, 2
Correct answer: B. 0, 0, 0, 0, 1, 1, 1, 2
The algorithm groups all 0s first, then all 1s, then all 2s, in one linear pass without a general comparison sort. For this input that gives [0, 0, 0, 0, 1, 1, 1, 2]. Sorting in descending order, [2, 1, 1, 1, 0, 0, 0, 0], is a different (and wrong) ordering.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Array [87, 4, 31, 36, 38, 63, 75] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- Array [56, 73, 74, 82, 90, 3, 48] is a sorted array rotated at pivot 2. Using the standard O(log n) rotated-array binary search (decide…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [1, 5, 5, 2, 5, 1, 5, 5]. What candidate and…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [6, 4, 4, 4, 6, 4, 6, 2]. What candidate and…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-3)…