0

So I am following this algorithm course from Princeton University done through coursera. I was doing this question related to stacks. The answer baffles me. Appreciate if anyone can kindly show me where I am going wrong in this;

Question : Suppose that an intermixed sequence of push and pop operations are performed on a LIFO stack. The pushes push the letters A through J in order; the pops print out the return value. Which of the following output sequence(s) could occur?

Correct answer 1 : B E D C G A F I J H

Explanation : When G is pushed, both A and F are still on the stack. So, F would be popped before A.

My question : If F should appear before A, then that answer cannot be true right?

Correct answer 2 : B C E A I J H G F D

Explanation : When E is pushed, both A and D are still on the stack. So, D would be popped before A.

My Question : again D should come before A right?

Appreciate if someone can show me what I might be doing wrong.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
dinukadev
  • 2,279
  • 17
  • 22

1 Answers1

0

You are right, both examples cannot happen, for the reasons you explain. Are you sure you are reading the answers correctly? The explanations are reasons why that output cannot happen, not why it can.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • Hi Keith, well it was ticked as a right answer and an explanation was given. Probably some mistake. Thank you for clarifying. I thought i was missing some point. – dinukadev Feb 16 '13 at 06:36