Data Structures & AlgorithmsVery hardProduct company OA
The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 2, 2, 2, 1, 1, 0] (values 0, 1, 2 only) in place. What is the resulting array?
- A. 0, 0, 2, 2, 2, 1, 1, 1
- B. 0, 1, 1, 2, 2, 2, 0, 1
- C. 0, 0, 1, 1, 1, 2, 2, 2
- D. 2, 2, 2, 1, 1, 1, 0, 0
Correct answer: C. 0, 0, 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, 0, 1, 1, 1, 2, 2, 2]. Sorting in descending order, [2, 2, 2, 1, 1, 1, 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
- What is the postfix (Reverse Polish) form of the infix expression "e (a + b)- h / d", built with the standard stack-based conversion (^…
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(2), enqueue(3), enqueue(3), enqueue(2), enqueue(3)…
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- Keys 26, 63, 80, 39, 14, 99, 18, 93, 44 are inserted via linear probing (h(k) = k mod 11, probe by +1 on collision) into an initially…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(3), enqueue(1), enqueue(6), enqueue(9), dequeue()…
- What is the postfix (Reverse Polish) form of the infix expression "i (f + c)- a / h", built with the standard stack-based conversion (^…