Skip to main contentSkip to solution

Suppose there are 3 elements 4, 2 and 7 to be input in the same order in the stack. In what sequence should the operation follow, so the final content of the stack from top to bottom is [7,4]?

(A) PUSH[4]

(B) POP

(C) PUSH[2]

(D) PUSH[7]

Solution

✅ Correct Option: 4

Elements go in the order 4, 2, 7, and a stack removes the most recent element first. To end with 7 on top of 4, the element 2 must be pushed and then popped before 7 is pushed.

OperationStack (top to bottom)
(A) PUSH[4]4
(C) PUSH[2]2, 4
(B) POP4
(D) PUSH[7]7, 4

Option 1: the POP removes 4, not 2, leaving [7, 2].

Option 2: the POP removes 4, and 2 is pushed last, giving [2, 7].

Option 3: starting with POP on an empty stack causes underflow.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question