ITPEC FE Morning October 2018 Question 6
ITPEC FE Morning October 2018 — Question 6 of 80
Stack Output Sequences — determining which output order is possible when data passes through a single stack.
Given input sequence A, B, C, D (pushed in order), we can pop at any point between pushes. To verify a candidate output, simulate the process:
Check (c) C, B, D, A:
1. Push A, Push B, Push C → stack: [A, B, C]
2. Pop → C (output: C)
3. Pop → B (output: C, B)
4. Push D → stack: [A, D]
5. Pop → D (output: C, B, D)
6. Pop → A (output: C, B, D, A) ✅
Why not others:
- (a) A, D, B, C — after popping A and pushing B, C, D, popping D leaves [B, C]; next pop gives C, not B
- (b) B, D, A, C — after popping B and D, stack is [A, C]; popping gives C before A, producing B, D, C, A instead
- (d) D, C, A, B — after pushing all four and popping D, C, stack is [A, B]; B is on top, so it pops before A, giving D, C, B, A instead
Key rule: once two elements are both in the stack, the one pushed later must come out first — you cannot skip over it.
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.