Data Structures & AlgorithmsHardIBM · Deloitte pattern
The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 2, 0, 0, 2, 1, 0, 2, 0] (values 0, 1, 2 only) in place. What is the resulting array?
- A. 0, 0, 0, 0, 2, 2, 2, 1, 1
- B. 0, 2, 0, 1, 2, 0, 0, 2, 1
- C. 0, 0, 0, 0, 1, 1, 2, 2, 2
- D. 2, 2, 2, 1, 1, 0, 0, 0, 0
Correct answer: C. 0, 0, 0, 0, 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, 0, 0, 1, 1, 2, 2, 2]. Sorting in descending order, [2, 2, 2, 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
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [3, 4, 4, 4, 4, 1, 3, 4, 4]. What candidate and…
- A recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(9)?
- 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…
- 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)…