ITPEC FE Morning April 2021 Question 7
ITPEC FE Morning April 2021 — Question 7 of 80
Stack output sequences — determine which output order is possible from a stack.
Input arrives as A, B, C, D. At each step we can push the next input or pop the top of stack.
Check (c) C, B, D, A:
- Push A, B, C → stack: [A, B, C]
- Pop C → output: C | stack: [A, B]
- Pop B → output: C, B | stack: [A]
- Push D → stack: [A, D]
- Pop D → output: C, B, D | stack: [A]
- Pop A → output: C, B, D, A ✅
Why not others:
- (a) A, D, B, C — after popping D, stack has [B, C] with C on top; need B but C blocks it
- (b) B, D, A, C — after popping D, stack has [A, C] with C on top; need A but C blocks it
- (d) D, C, A, B — must push all four, then pop gives D, C, B, A; cannot swap B and A
Key rule: a sequence is not producible if it requires popping a lower element while a higher one still sits on top.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.