Data Structures & AlgorithmsHardProduct company OA
The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 1, 2, 2, 1, 1, 1, 1, 2, 0] (values 0, 1, 2 only) in place. What is the resulting array?
- A. 0, 2, 2, 2, 1, 1, 1, 1, 1, 1
- B. 2, 2, 2, 1, 1, 1, 1, 1, 1, 0
- C. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2
- D. 0, 2, 1, 1, 1, 1, 2, 2, 1, 1
Correct answer: C. 0, 1, 1, 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, 1, 1, 2, 2, 2]. Sorting in descending order, [2, 2, 2, 1, 1, 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
- Heights [8, 6, 3, 2, 6, 8, 8, 4, 7, 1] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Kadane's algorithm runs on the array [5, 4, 4, -3, -5, -1, 8] to find the maximum sum of a contiguous subarray. What sum does it return?
- What is the sum of the minimum element over every contiguous subarray of [9, 4, 5, 3, 1, 9, 8, 1]?
- QuickSort (last element as pivot, Lomuto-style partition) sorts [31, 34, 38, 59, 23, 16, 54, 42, 46]. Counting one comparison per element…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is left-rotated by d = 2 positions using the reversal algorithm (reverse first d, reverse rest…
- Two sorted linked lists [9 → 14 → 35 → 36 → 37] and [27 → 29 → 31 → 32] are merged with the standard O(n+m) two-pointer merge (always take…