Data Structures & AlgorithmsVery hardProduct company OA

An array is split into the MAXIMUM number of contiguous chunks such that sorting each chunk separately and concatenating them gives the fully sorted array. How many chunks are possible for [3, 1, 2, 6, 4, 5, 7, 9, 8]?

  1. A. 3
  2. B. 5
  3. C. 9
  4. D. 4

Correct answer: D. 4

For a permutation of 1..n, a chunk can end at position i exactly when max(prefix) = i. Prefix maxima: 3, 3, 3, 6, 6, 6, 7, 9, 9 → equal to i at i = 3, 6, 7, 9 → chunks [3 1 2] [6 4 5] [7] [9 8]. Forgetting that [7] stands alone gives 3.

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

Exams that ask this topic