Data Structures & AlgorithmsHardProduct company OA · Infosys pattern

For each element of [4, 10, 5, 8, 20, 15, 3, 12], find the nearest SMALLER element to its LEFT (−1 if none) using a monotonic stack. What is the result?

  1. A. −1, 4, 4, 5, 8, 15, −1, 3
  2. B. −1, 4, 4, 5, 8, 8, −1, −1
  3. C. −1, −1, 4, 5, 8, 8, −1, 3
  4. D. −1, 4, 4, 5, 8, 8, −1, 3

Correct answer: D. −1, 4, 4, 5, 8, 8, −1, 3

Keep an increasing stack, popping every value ≥ the current one. 5 pops 10 → answer 4. 15 pops 20 → answer 8 (not 20, and not 15 itself). 3 empties the stack → −1. 12 sees 3 on top → 3. The option with 15 for 15's slot forgets to pop larger values; the one ending −1 forgets that 3 stays on the stack.

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

Exams that ask this topic