Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard
The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 1, 2, 2, 2, 1, 1] (values 0, 1, 2 only) in place. What is the resulting array?
- A. 0, 1, 1, 1, 1, 2, 2, 2
- B. 1, 1, 2, 2, 2, 1, 0, 1
- C. 0, 2, 2, 2, 1, 1, 1, 1
- D. 2, 2, 2, 1, 1, 1, 1, 0
Correct answer: A. 0, 1, 1, 1, 1, 2, 2, 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, 1, 1, 1, 1, 2, 2, 2]. Sorting in descending order, [2, 2, 2, 1, 1, 1, 1, 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
- Elevation map [6, 1, 1, 0, 3, 3, 5, 5, 2, 0] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…
- Array [75, 79, 80, 82, 6, 11, 23, 40, 63, 67] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary…
- Array [1, 2, 3, 4, 5, 6, 7, 8] is left-rotated by d = 7 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- What is the sum of the minimum element over every contiguous subarray of [7, 6, 6, 4, 8, 9]?
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "BABAACBCCA" are anagrams of "AB"?
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(7), enqueue(2), dequeue(), enqueue(5), dequeue()…