Data Structures & AlgorithmsModerateWipro · Accenture pattern

An array has only 0s, 1s and 2s. The Dutch National Flag algorithm sorts it in one pass. What are its time and extra space?

  1. A. O(n) time, O(1) space
  2. B. O(n²) time, O(1) space
  3. C. O(n log n) time, O(1) space
  4. D. O(n) time, O(n) space

Correct answer: A. O(n) time, O(1) space

Three pointers (low, mid, high) split the array into 0s, unknown, and 2s; each step shrinks the unknown part by one, so it runs in O(n) time with only a few variables of extra space.

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

Exams that ask this topic