IB Computer Science B2.2: Data structures

New IB Computer Science syllabus · first exams May 2027 · Theme B · Paper 2 · SL & HL · Python and Java

Choosing a data structure is a design decision, not just a syntax choice. B2.2 covers fixed and growable structures, and two structures that deliberately restrict how data goes in and out: stacks and queues.

StatementTopicCommand term
B2.2.1Static vs dynamic data structuresCompare
B2.2.2Arrays and ListsConstruct
B2.2.3Stacks (LIFO)Explain
B2.2.4Queues (FIFO)Explain

Key points

B2.2.1 Static vs dynamic

  • Static (an array): memory is allocated once, at a fixed size. It's fast and predictable, but it can't grow. To grow it, you have to create a bigger array and copy the data across.
  • Dynamic (a list): grows and shrinks automatically, with its storage reallocated behind the scenes. It's flexible, but has some overhead.
  • Compare them on speed, memory use and flexibility.

B2.2.2 Arrays and Lists

  • Java: 1D and 2D arrays, and ArrayList.
  • Python: 1D and 2D lists.
  • You must be able to add, remove and traverse elements in a dynamic list.
  • A 2D structure is indexed as [row][col].

B2.2.3 Stacks: last in, first out

  • Operations: push, pop, peek, isEmpty.
  • Uses: undo features, browser history, call stacks.

B2.2.4 Queues: first in, first out

  • Operations: enqueue, dequeue, front, isEmpty.
  • Uses: printer queues, task scheduling.
  • Always check isEmpty before dequeue, to avoid a runtime error.

Exam-style question

Trace the contents of an initially empty stack after the following operations, showing the contents after each step: push(5), push(8), pop(), push(3), peek(). [5]

Mark scheme

[1] per correct step:

OperationStack (bottom → top)Returns
push(5)[5]n/a
push(8)[5, 8]n/a
pop()[5]8
push(3)[5, 3]n/a
peek()[5, 3] (unchanged)3

Common mistakes that cost marks

  • Thinking peek removes the item. It only looks at the top item.
  • Mixing up LIFO and FIFO in scenario questions. An undo feature needs a stack; a print queue needs a queue.
  • Indexing the centre of a 3×3 grid as [2][2]. It's [1][1], because indexes start at 0.

Frequently asked questions

Do Java students need ArrayList?
Yes. The guide names 1D arrays, 2D arrays and ArrayLists for Java, and 1D and 2D lists for Python.

Which stack and queue operations must I know?
Stacks: push, pop, peek and isEmpty. Queues: enqueue, dequeue, front and isEmpty.


Go further