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?

  1. A. 0, 0, 0, 0, 2, 1, 1, 1
  2. B. 0, 0, 0, 0, 1, 1, 1, 2
  3. C. 2, 1, 1, 1, 0, 0, 0, 0
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic