ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Stack s = empty, mins = empty
Procedure push2(Integer x)
    push(s, x)
    if (mins is empty OR x <= top(mins)) push(mins, x)
End Procedure
Procedure pop2()
    if (top(s) == top(mins)) pop(mins)
    pop(s)
End Procedure
push2(5) push2(3) push2(7) push2(3) pop2() Print top(mins)
pop2() pop2() Print top(mins)
push2(2) Print top(mins)
  1. A. 3 5 2
  2. B. 3 3 2
  3. C. 5 5 2
  4. D. 3 7 2

Correct answer: A. 3 5 2

The second 3 was also pushed on mins (because of <=), so after the first pop the minimum is still 3. Popping 7 and then 3 leaves only 5 in both stacks: min 5. Pushing 2 makes it 2. Output 3 5 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic