IB Computer Science HL · Year 2 · Lesson 2

Stacks: LIFO Operations and Trade-offs

Explain stack behaviour, trace the four required operations, and justify when a LIFO structure fits a problem.

Paper 250 minutesB2.2.3
Today’s targets

What you need to be able to do

2 / 9
  • explain a stack as a last-in, first-out (LIFO) structure;
  • use and trace push, pop, peek and isEmpty accurately;
  • explain how stack operations affect performance and memory use;
  • justify whether a stack is appropriate for a specific problem.
B2.2.3
Paper 2 habitRead the command term and the data given. A technically correct answer still needs to answer the exact task.
Learn + retrieve

Rapid Recall Deck

3 / 9

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

B2.2.3 · Explain

Read a stack from the top

4 / 9
Starting stackBottom → [A, B, C] ← Top. The stack then performs push(D), peek(), pop(), push(E), pop().

Trace the operations

After each operation — write the entire stack from bottom to top.
For peek/pop — also state the value returned. After the five operations, state the result of isEmpty().
  1. push(D) → [A, B, C, D]
  2. peek() returns D; stack stays [A, B, C, D]
  3. pop() returns D; stack becomes [A, B, C]
  4. push(E) → [A, B, C, E]
  5. pop() returns E; stack becomes [A, B, C]

isEmpty() returns False because three items remain.

Operations

Explain what each operation changes

5 / 9

Push / pop

These operations change the number of stored items.

Push — add one item at the top.
Pop — remove and return the top item.

Peek / isEmpty

These operations inspect the stack without changing its contents.

Peek — read the top item without removing it.
isEmpty — check whether there are zero items.

Explain the LIFO rule

A student says, “A stack is just a list with a different name.” Explain what restriction makes a stack different as an abstract behaviour.

Access point — identify where normal insertion and removal are allowed.
Removal order — identify which saved item leaves first after several pushes.
Conclusion — connect that behaviour to the ordering rule.

A stack restricts normal insertion and removal to one end, called the top. Because a new item is pushed onto the top and pop removes that same top item, the most recently added item is the first one removed, giving last-in, first-out behaviour.

Performance + memory

What do stack operations cost?

6 / 9

A stack only needs direct access to its top. With an appropriate implementation, push, pop, peek and isEmpty can be performed without scanning every stored item.

PerformanceTop-only access avoids shifting or searching through all elements for the core operations. Some implementations may occasionally need extra work to grow storage capacity.
MemoryThe stored items consume memory, and the chosen implementation can add overhead—for example unused reserved capacity or links between nodes.

Explain the trade-off

A program's undo stack grows from 10 saved actions to 10,000 saved actions. Explain one likely effect on memory use and one reason stack operations can still remain efficient.

Memory — connect more stored items to greater storage use.
Performance — connect normal operations to access at the top rather than a full scan.

Memory use will increase because many more undo records must be stored. The core stack operations can still remain efficient because push, pop and peek work at the top of the structure and do not require searching through all 10,000 records.

Choose the structure

When does LIFO match the problem?

7 / 9
ScenarioA drawing app records actions so that Undo always reverses the most recent action that has not already been undone.

Explain why a stack is appropriate

A drawing app records actions so that Undo must reverse the most recent action that has not already been undone. Explain why a stack matches this requirement.

Stored action — identify which saved action must be reached first.
Operation — name the stack operation that retrieves/removes that action.
Contrast — explain what would go wrong with oldest-first processing.

Each new drawing action can be pushed onto the top of the stack. When Undo is selected, pop removes the most recent action, which is exactly the action that must be reversed first. A FIFO structure would instead remove the oldest saved action first, so it would not match the Undo requirement.

Appropriateness is contextualDo not say “stacks are faster” as a generic reason. Explain why LIFO matches the order required by the specific problem.
Exam lens

Teacher-created Paper 2 practice

8 / 9

1. A stack initially contains, from bottom to top, [4, 7, 9]. Trace: push(12), pop(), peek(), push(15).

2. Explain how peek() differs from pop().

3. A program processes nested function calls. Explain why a stack is a suitable structure for remembering which unfinished call should resume next.

  1. push(12) → [4,7,9,12]; pop() returns 12 → [4,7,9]; peek() returns 9 with no change; push(15) → [4,7,9,15].
  2. peek() returns the top item without removing it; pop() returns and removes the top item.
  3. The most recently started unfinished call must complete or return before the earlier call resumes, which matches LIFO. New calls are pushed; returning from a call pops it and exposes the previous call.
Homework

Finish the learning cycle

9 / 9

IA — main task

Refine the current IA problem/planning material. Make sure the data the solution must store and manipulate is described clearly enough to support later design decisions.

Syllabus — short retrieval

Repeat only the stack cards marked “Review again”, then redo the trace on page 8 without revealing the marking points.