Data Structures & AlgorithmsModerateGATE CS · TCS NQT

Process [5, 3, 6, 2, 7, 1] with a stack: before each push, pop every element that is greater than or equal to the new value. How many elements remain on the stack at the end?

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

Correct answer: B. 1

Each new value pops every larger-or-equal value above it, and the stack ends as just [1], so its size is 1.

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

Exams that ask this topic