Teacher Guide: Data Structures
Student page: Data Structures IB Syllabus: B2.2 Suggested timing: 8–10 periods (90 min each)
Contents
Sub-page Guides
Each sub-page below has its own dedicated teacher guide with period-by-period lesson plans, answer keys for all practice exercises, differentiation strategies, and integration notes.
| Sub-page | Syllabus | Student Page | Teacher Guide |
|---|---|---|---|
| 1D Arrays | B2.2.2 | Student page | Teacher guide |
| 2D Arrays | B2.2.2 | Student page | Teacher guide |
| ArrayList | B2.2.3 | Coming soon | Coming soon |
| Stacks | B2.2.4 | Coming soon | Coming soon |
| Queues | B2.2.5 | Coming soon | Coming soon |
Teaching Sequence
Arrays (B2.2.2)
Unplugged — mailbox analogy:
Before any code, draw a row of numbered mailboxes on the board: box 0, box 1, box 2, box 3, box 4. Each holds exactly one piece of mail. Ask: “Why start at 0?” Explain that the index is an offset from the start of the block in memory — box 0 is at offset 0 from the beginning, box 1 is 1 away, etc. This physical grounding prevents many off-by-one errors.
Ask: “If there are 5 boxes, what is the last index?” Students should say 4 (length − 1). Then ask: “What happens if you ask for box 5?” — the off-by-one problem made concrete before they see any error message.
Declaration and initialisation: Contrast two forms:
int[] scores = new int[5]; // size 5, all zeros
int[] scores = {85, 72, 91, 60, 78}; // known values
Ask: “When would you use each?” First: when you don’t know values yet (e.g., will be filled by user input). Second: when you know them at compile time.
Traversal pattern — establish this as the canonical form:
for (int i = 0; i < arr.length; i++) { ... }
Drill this until it is automatic. Every array operation they will ever write uses this skeleton.
Common operations — derive, don’t give:
For each operation (max, min, sum, count), ask students to describe the algorithm in words first:
- “How would you find the largest number in a list of numbers, looking at them one by one?” → seed with first element, update if larger
- “How would you count how many numbers are above 50?” → counter, increment conditionally
Write the code together as a class after they have described the logic.
The reference trap (B2.2.2)
Discovery activity — do not explain first:
Ask students to run this:
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);
Ask them to predict the output first. Almost everyone expects 1. They get 99. The surprise is the lesson.
Ask: “What does this tell you about what b = a actually does?” Guide them to: it copies the address, not the data. Draw a memory diagram — two variable boxes (a and b) both pointing to the same array block.
Then show the correct element-by-element copy. Ask: “Why does this work when b = a didn’t?” The difference is visible in the diagram.
Resize pattern: Connect to ArrayList. “ArrayList does this automatically — it creates a larger internal array and copies the data across every time the capacity is exceeded. Understanding this manually is the point of the exercise.”
2D arrays (B2.2.2) — SL and HL
Visual first: Draw a grid on the board (3 rows × 4 columns). Label rows 0–2, columns 0–3. Ask: “How many elements total?” Students count: 12. “What index is the element in row 1, column 2?” (matrix[1][2]).
Useful real-world contexts: grade book (students × assignments), game board, multiplication table, seating chart.
Traversal — outer loop rows, inner loop columns is the standard. Ask: “What would happen if you swapped the loops?” — students should reason that you would traverse by column first. Neither is wrong, but row-major is conventional for 2D arrays in Java.
ArrayList (B2.2.3)
Contrast with array — make the difference concrete:
Ask: “What if you don’t know how many students will be in the class?” An array requires a fixed size. ArrayList doesn’t. Show add() adding elements and size() returning the current count (not a fixed length). Emphasise .size() vs .length — students mix these up constantly.
Wrapper types: ArrayList<Integer> not ArrayList<int>. Ask: “Why do you think Java doesn’t allow primitive types in an ArrayList?” Brief answer: ArrayList stores object references; int is not an object. Java’s autoboxing handles the conversion automatically. Do not dwell — more detail in OOP.
Stacks (B2.2.4)
Unplugged motivation — Ctrl+Z:
Ask: “What does Ctrl+Z do in a word processor?” Students know: undo. Ask: “If you press Ctrl+Z three times, what is undone first?” The most recent action. Ask: “What kind of data structure would let you always undo the most recent action?” Let students describe it — they are describing a stack. Then name it: Last In, First Out.
Physical demonstration: Use a stack of books or a plate dispenser (if available). You only interact with the top. You cannot access the second book without removing the first. That constraint IS the stack.
Four operations only: push, pop, peek, isEmpty. Drill these with simple trace exercises before any code. Students draw the stack after each operation.
IB note: Exam questions about stacks are almost always abstract trace questions, not code-writing questions. Students need to be able to trace operations and show the stack state after each one.
Queues (B2.2.5)
Unplugged motivation — printer queue:
“You send three documents to the printer. Which prints first?” Students immediately say “the first one.” “What if the printer used a stack?” Laughter — the most recently sent document would print first, which would be absurd.
“What word do we use in English for this kind of waiting line?” Queue. First In, First Out.
Connect to OS: The process scheduler in the operating system uses a queue. The CPU serves processes in order of arrival (simplified). Students saw this in the OS unit — reinforce the cross-layer connection.
Four operations: enqueue (add), dequeue (remove front), front/peek, isEmpty. Same drill approach as stacks — trace before code.
Common confusion: Students mix up pop/peek with dequeue/poll because Java uses different method names (poll() for dequeue, peek() for front). Emphasise the concept names (enqueue/dequeue) separately from the Java API names.
Choosing the right structure
Pose scenarios and ask students to justify their choice:
- “You are building an undo feature for a text editor.” → Stack
- “You are managing a print queue.” → Queue
- “You are storing the 12 months of weather data.” → Array (fixed size, fast access by month)
- “You are building a contact list that grows as users add friends.” → ArrayList
- “You are representing a game board.” → 2D Array
Common Student Mistakes
| Mistake | Description |
|---|---|
arr[arr.length] | Off by one — last valid index is arr.length - 1 |
int[] b = a | Reference copy, not data copy — modifying b also modifies a |
ArrayList<int> | Must use ArrayList<Integer> (wrapper type) |
.length vs .size() | Arrays use .length (no parentheses); ArrayList uses .size() |
pop() on empty stack | Throws EmptyStackException — always check isEmpty() first in real programs |
Confusing peek() and pop() | peek looks without removing; pop removes and returns |
Queue: poll() vs peek() | poll() removes and returns front; peek() just looks |
IB Paper 2 Exam Tips
- Stack and queue trace questions are common and straightforward marks. Students must show the data structure contents after EACH operation, not just the final state.
- Know the four operations for each: push/pop/peek/isEmpty for stacks; enqueue/dequeue/front/isEmpty for queues.
- Array off-by-one is the single most common array error in student code. Exam questions sometimes deliberately include off-by-one code and ask students to identify the error.
- For 2D arrays:
matrix[row][col]— row first, column second. This is tested directly. - Students must explain why to choose a stack vs queue for a given problem. “Because it uses LIFO/FIFO” alone is insufficient — connect it to the problem’s requirements.
ArrayListvs array: the key difference is dynamic sizing. Use ArrayList when size is unknown at compile time.