Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

Using a monotonic (increasing) stack to find, for each element of [20, 17, 18, 14, 44, 28], the nearest element to its left that is strictly smaller (or -1 if none), what is the resulting array?

  1. A. [44, 18, 44, 44, -1, -1]
  2. B. [-1, -1, 17, -1, 14, 14]
  3. C. [14, 14, -1, 17, -1, -1]
  4. D. [14, -1, -1, -1, 14, 17]

Correct answer: B. [-1, -1, 17, -1, 14, 14]

Scan left to right, popping stack entries that are >= the current value, then the new top (if any) is the answer for that position: [-1, -1, 17, -1, 14, 14]

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

Exams that ask this topic