/en-tutor-prep-ib-cs-data-structures-arrays-lists-stacks-queues/
/en-tutor-prep-ib-cs-data-structures-arrays-lists-stacks-queues/
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

IB Computer Science: Data Structures — Tutor Lesson Plan (arrays, lists, stacks and queues; 1,023 copies for 1,024 appends)

IB Computer Science: Data Structures — Tutor Lesson Plan (arrays, lists, stacks and queues; 1,023 copies for 1,024 appends)

  • 2026-11-28

Every student can loop over a list, but ask how many items shift when one is inserted in the middle and the room goes quiet. Data structures are about cost: the same job moves 1,000 items in an array and none in a stack.

Objectives — the student can (1) contrast static and dynamic structures: a 40-slot array occupies 160 bytes and cannot take slot 41; a dynamic list resizes when full; (2) compute 2D indexing: 30 rows by 5 columns is 150 cells and 600 bytes, [3][2] maps to 3 times 5 plus 2, which is 17, and the last index is 149; (3) sum rows and columns: [323, 315, 339] and [209, 195, 190, 205, 178], a total of 977 and a first-row mean of 64.6; (4) state the cost of append and insert: 1,024 appends copy 1,023 items (2.0 writes per append), a front insert shifts 1,000, a middle insert 500 and an append 0; (5) trace a stack: five pushes and two pops leave 3, brackets {[()]} reach depth 3 and ([)] fails at index 3; (6) trace a queue: 12 people plus 3 minus 5 leaves 10, and a 8-slot circular queue at front 3 and size 5 has rear 0, becoming rear 2 and size 7 after two more.

01 Prerequisites (3-minute check)

If any of these fails, fix it before the lesson starts.

Skill Quick check Pass mark
Zero-based indexing a list of 5 items, what is the last index? 4
Integer division 150 cells in 5 columns, how many rows? 30
Order of service a stack of plates and a queue of people, which is first in first out? the queue; plates are last in first out

02 Lesson flow (60 minutes)

Time Stage Tutor does Student does
0-7 min Opening check how many items shift when one is inserted in the middle? estimate now, compare with the numbers at the end
7-22 min Static, dynamic, 1D and 2D arrays 40 slots, a 30 by 5 table compute bytes and indices
22-36 min List add, remove and traverse 1,024 appends, a front insert count the shifts
36-48 min The stack, LIFO five pushes, two pops, depth 3 trace push and pop
48-56 min The queue, FIFO, and a circular buffer 10 people, 8 slots, front and rear compute front, rear and size
56-60 min Exit check three questions marked on the spot work alone

03 Teaching script (read it out)

Scene 1: Static and dynamic: memory and its price

Open with lockers: the school has 40, and int[40] occupies 40 times 4, which is 160 bytes. Student 41 cannot fit, so a new array of 41 must be allocated and all 40 values copied across.
A static structure wins because the address of item i is the start plus i times 4, so reading and writing are constant time; it loses because the size must be known in advance, and guessing high wastes memory while guessing low blocks the class. A dynamic structure grows on demand and pays for it by copying when it resizes.
Put a number on resizing. Starting at capacity 1 and doubling whenever full, reaching 1,024 items triggers resizes at 1, 2, 4, 8 and so on to 512, copying 1,023 items in all. Add the 1,024 writes of the items themselves and the total is 2,047, or 2.0 writes per append on average. That is amortised constant time, not zero cost.
Then 2D arrays: 30 students by 5 subjects is 150 cells and 600 bytes, still one contiguous block. Row-major indexing is row times 5 plus column, so [3][2] is 3 times 5 plus 2, which is 17, and the last cell [29][4] is 149. Ask the class to compute [0][4], which is 4, and [4][0], which is 20: the two are different places, so rows and columns cannot be swapped.

Student task: change the 30 by 5 table to 30 by 6, recompute the cell count, the bytes and the index of [3][4], and say in one sentence why row-major multiplies by the column count.

Scene 2: Lists: adding, removing and traversing, where shifts are the cost

Start with a list of 8 names and remove the one at index 2: the 5 names after it each move one place, so removal costs 5 shifts, not one. Scale to 1,000 items: a front insert shifts 1,000, a middle insert 500 on average and an append 0. That set of numbers is the whole reason arrays read well and edit badly.
Traversal is easy: five values 20, 35, 50, 15 and 100 sum to 220, average 44.0, with the maximum 100 at index 4. Ask for three loops: for v in a list when the value is needed, for i in range(len(a)) when the index is needed, and a nested pair of loops for a 2D table, outer for rows and inner for columns.
Then memory. A Python list stores an 8-byte pointer to a 28-byte integer object per item, so 1,000 items take 36,000 bytes; the same 1,000 integers in an array of 4-byte values take 4,000, a factor of 9.0. A Java ArrayList holds object references and carries the same overhead. The lesson is that convenience and compactness pull in opposite directions.
Finish with the queue trap saved for scene three: a Python list used as a queue with pop(0) shifts 999 items every time, so a long queue crawls, while deque.popleft is constant time. That contrast turns choosing a structure from a slogan into arithmetic.

Student task: for a 10-item list, state how many items shift when index 7 is removed and when an item is inserted at the front, then write one sentence on choosing the structure that matches the commonest operation.

Scene 3: Stacks and queues: order decides purpose

Start with the stack, LIFO: push A, B, C, D and E, then pop twice. The pops return E and D and 3 items remain, C, B and A, so the next peek shows C. isEmpty is True before the first push and True again once everything is popped. Ask for three real uses: the browser back button, editor undo (up to 100 steps, with 30 undone and 70 left) and bracket checking.
Bracket checking is an exam favourite: over the six characters {[()]}, push each opening bracket and pop the matching one on each close; the maximum depth is 3 and an empty stack at the end means correct. With ([)] the attempt at index 3 wants to pop a square bracket but the top is a round one, so the failure is one of order, not of count.
Then the queue, FIFO: 12 people waiting, 3 joining and 5 served leaves 10; dequeue always takes the front and enqueue always adds at the back. A 8-slot circular queue keeps a front index and a size, and rear is front plus size modulo 8. With front 3 and size 5, rear is 0, which wraps to 0; two more make the size 7 and the rear 2. Ask when it is full: size equal to 8 puts rear back at front, so a separate size, or one deliberately empty slot, is needed to tell full from empty.
Close on speed. Stack push and pop are constant time; a list used as a queue shifts 999 items per dequeue while a deque does not; an unsorted search averages 500 comparisons and can take 1,000, whereas a binary search over sorted data needs 10 (2 to the power 10 is 1,024). Choosing a structure is choosing between these numbers.

Student task: in an eight-slot circular queue with front at 6 and size 7, find the rear, then add one item and give the new front, rear and size, and say in one sentence why rear equals front when the queue is full.

04 Question types and worked steps

Type Example Steps Answer
Compute a 2D index and bytes 30 rows by 5 columns, 4 bytes per cell three steps: cells equals rows times columns, bytes equals cells times cell size, and the row-major index is row times columns plus column 150 cells, 600 bytes, [3][2] is 17
Compute append and insert shifts 1,024 appends, a front insert into 1000 items two steps: sum the doubling copies 1, 2, 4 and so on, then count the items after the insertion point 1,023 copies for the appends (2.0 per append); front insert 1,000, middle 500
Trace a stack push A B C D E, pop twice, then peek three steps: draw the stack bottom to top, pop from the top, peek without removing pops E and D, 3 remain, peek shows C
Compute front, rear and size in a circular queue 8 slots, front 3, size 5 two steps: rear equals front plus size modulo the slot count, then recompute after adds and removes; full means size equals the slot count rear 0; after two adds, size 7 and rear 2
Choose a structure undo, a print queue, reading the 500th item, frequent front inserts one answer each: last in first out is a stack, first in first out is a queue, random access is an array, frequent front inserts use a linked structure or deque stack, queue, array in constant time, deque to avoid 999 shifts
Sanity check (teach this): five checks: (1) a 2D index must be below the cell count, so [3][2] is 17 out of 150 and an answer of 152 means a multiplication error; (2) a front insert shifts every item (1,000) and can never shift fewer than a middle insert at 500; (3) a stack and a queue reverse each other: on A to E the stack returns E and D while the queue returns A and B; (4) the size of a circular queue lies between 0 and 8, so 7 is legal and 9 is not; (5) the 500 average linear comparisons against 10 for binary search only holds if the data is sorted. The commonest misjudgement is to generalise from append to every list operation: append costs 2.0 writes amortised, but a front insert shifts 1,000 items, three orders of magnitude apart.

05 Independent practice (3 questions)

Question Answer What to watch
30 students by 5 subjects. (a) How many cells and how many bytes at 4 bytes each? (b) What is the linear index of [3][2]? (a) 150 cells and 600 bytes; (b) 17 the index is row times columns plus column, not row plus column times rows
A 8-slot circular queue has front at 3 and size 5. (a) Where is the rear? (b) After two enqueues, what are the size and the rear? (a) 0; (b) size 7 and rear 2 the rear wraps modulo the slot count, and wrapping to 0 is normal
A stack receives pushes of 20, 35 and 50, then one pop and one peek. (a) What does the pop return? (b) What does the peek return? (c) How many items remain? (a) 50; (b) 35; (c) 2 peek reads without removing, so the top after the pop is the answer

06 Common mistakes and what to say

Mistake Student writes Say this
Swapping rows and columns answering that [3][2] is 3 plus 2 times 30, which is 63 row-major is row times the column count plus the column, so 3 times 5 plus 2 is 17. Your version is column-major, used by Fortran and some scientific tools only; the syllabus means row-major.
Assuming append is always free answering that append is O(1) so every list operation is fast append costs 2.0 writes amortised because a full list copies 1,023 items, and a front insert shifts 1,000. Say which operation is fast.
Using a list as a queue and removing from the front writing queue.pop(0) as dequeue pop(0) shifts 999 items every call and crawls on a long queue; deque.popleft is constant time. IB asks for the cost, not only for working code.
Reversing stack and queue behaviour answering that a queue returns the most recent arrival a queue is first in first out, so the front of the 12 leaves first; last in first out is the stack. The canteen queue never serves from the back.

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
Index [3][2] is 17 of 150 an answer of 63 means rows and columns swapped
Stack five pushes and two pops leave 3, depth 3 wrong order means restart scene three
Queue front 3 plus size 5 gives rear 0 no modulo means not finished

09 Tutor FAQ

Does IB want arrays or lists?

Both, with the difference stated: an array has a fixed size, allows address arithmetic and constant-time access, while a list grows but pays amortised cost on append and shifts on a front insert. B2.2.2 names 1D and 2D arrays plus Java ArrayList and Python list.

Must stacks and queues be implemented?

Know how to build them from an array or list and state the cost of each operation. A stack on the end of a list is constant time; a queue that removes from the front of a list is O(n), so two indices or a deque are needed. Exams mostly trace and choose, with basic operations for implementation.

Is Big O needed in this chapter?

Only the ideas of constant time and proportional to n; the formal treatment is B2.4. The numbers here are enough: 2.0 writes per append, 1,000 shifts for a front insert, and 500 linear comparisons against 10 for binary search.

Find a tutor — freeTutor Registration →

A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →