OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

Given a stack S: 3 2 of capacity 3.

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Given a stack S: 3 2 of capacity 3. Which one of the given options is correct about the stack S after below operations?

Push(2), Peek(), Pop(), Pop(), Pop(), Peek()

Choose one option.
Show answer & explanation
Answer: D. Underflow

The stack starts with [3, 2] (size 2/capacity 3). After Push(2), Peek(), and Pop(), we have [3, 2]. The next three operations are Pop() (removes 2), Pop() (removes 3), and Pop() (stack is empty). The final Peek() on an empty stack triggers an underflow error—attempting to access data from an empty stack.

Step-by-step Derivation:
Initial state: Stack S = [3, 2], size = 2, capacity = 3

  1. Push(2): S = [3, 2, 2], size = 3 (no overflow—within capacity)
  2. Peek(): Returns 2, stack unchanged: S = [3, 2, 2]
  3. Pop(): Removes 2, S = [3, 2], size = 2
  4. Pop(): Removes 2, S = [3], size = 1
  5. Pop(): Removes 3, S = [], size = 0 (stack now empty)
  6. Peek(): Attempts to peek at an empty stack → UNDERFLOW

The stack is empty when trying to read the top element, causing an underflow condition.