Skip to study material
← Question index

QUESTION 06

Trace the supplied sequence of stack operations and find the final peek.

5 marks

Answer in brief

A stack trace updates the logical top after every operation while preserving the untouched bottom portion.

Initial state

Bottom to top: 10, 20, 50, 30, 90, 60. Capacity is 10 and the initial count is 6. The top is 60.

Operation trace

  1. pop removes 60: top becomes 90.
  2. push(30): top becomes 30.
  3. push(40): top becomes 40.
  4. pop removes 40: top becomes 30.
  5. push(60): top becomes 60.
  6. pop removes 60: top becomes 30.
  7. pop removes that pushed 30: top becomes 90.
  8. pop removes 90: top becomes the original 30.

Final state

Bottom to top: 10, 20, 50, 30. Peek is 30 and does not remove it. The count is 4. None of these operations reaches empty or exceeds capacity, so there is no underflow or overflow.

How to avoid mistakes

Record the complete remaining sequence or a clear top marker after each step. Distinguish a newly pushed 30 from an earlier equal value if labels help, but the final requested value is still 30. Never remove the bottom item for pop; that would violate LIFO.

Complexity

For k operations on the bounded array representation, each operation is O(1), so the trace requires O(k) work. This specific trace has a fixed eight operations. Writing every intermediate full stack for display can add output work beyond the stack operations themselves.

Related exam questions

The following are topic matches from the supplied papers. Marks are their printed allocations, separate from the practice answer badge.

  • August/September 2022 · Q1(a) · 8 marks

    Starting stack bottom-to-top: 10, 20, 50, 30, 90, 60; capacity 10. Apply pop, push(30), push(40), pop, push(60), pop, pop, pop. Find the final peek value.

    Paper DS02 · page 1
  • February 2024 · Q3(a) · 5 marks

    Starting stack bottom-to-top: 10, 20, 50, 30, 90, 60; capacity 10. Apply pop, push(30), push(40), pop, push(60), pop, pop, pop. Find the final peek value.

    Paper DS03 · page 1

View all matching source questions →