Skip to content
9618Paper 4 · Practical§19.1, §19.2

19. Computational Thinking and Problem-Solving

Recursion, ADTs in code (stack, queue, linked list, binary tree, dictionary, hash table), searching and sorting (binary search, insertion sort, bubble sort), and Big O.

Statometer80BankerNext Paper 484%
Marks a paper29.4 · 39%Rank#2 of 8 · #2 on P4Trend · last 12Oct/Nov 24 · 41: 19 marksOct/Nov 24 · 42: 29 marksOct/Nov 24 · 43: 19 marksMay/Jun 25 · 41: 50 marksMay/Jun 25 · 42: 0 marksMay/Jun 25 · 43: 49 marksOct/Nov 25 · 41: 20 marksOct/Nov 25 · 42: 26 marksOct/Nov 25 · 43: 20 marksMay/Jun 26 · 41: 26 marksMay/Jun 26 · 42: 45 marksMay/Jun 26 · 43: 46 marks
8 in 10 chance in the next paper

Everything for this topic — study hub

A2 Level · 9618 · Paper 4

Statometer — what 64 real papers say about this topic and each of its 3 syllabus bullets

Banker · #2 of 8 in A2 Level · recomputed with every new session

80BANKER
Banker#2 of 8 in A2 Level#2 on Paper 4 Steady

Set in nearly every paper and worth a big slice of it — revise first, expect it.

Next Paper 4
84%
8 in 10 chance it is set
Marks a paper
29.4 / 75
39% of Paper 4 · fair share 50%
Appeared in
28 / 32
Paper 4 sittings 20212026
Last set
May/Jun 2026
9618/43 · Q3 · 25 marks · 6-series streak
Marks in each of the last 12 Paper 4 sittingsOct/Nov 24May/Jun 26
Oct/Nov 24 · 41: 19 marksOct/Nov 24 · 42: 29 marksOct/Nov 24 · 43: 19 marksMay/Jun 25 · 41: 50 marksMay/Jun 25 · 42: 0 marksMay/Jun 25 · 43: 49 marksOct/Nov 25 · 41: 20 marksOct/Nov 25 · 42: 26 marksOct/Nov 25 · 43: 20 marksMay/Jun 26 · 41: 26 marksMay/Jun 26 · 42: 45 marksMay/Jun 26 · 43: 46 marks

What the papers say

  • Set in 28 of 32 Paper 4 sittings — about 9 papers in 10.
  • Worth about 29.4 marks a paper (39% of Paper 4).
  • Also turns up in Paper 3 (100% of sittings, ~13.3 marks) — usually inside a scenario question.
  • Last set May/Jun 2026 · 9618/43 · Q3 for 25 marks — in the most recent series.
  • Set in each of the last 6 series without a miss.
  • Steady at around 33.7 marks a paper year on year.
  • Lives on “Write” and “Complete” — 94% of its questions: you must produce something — code, a diagram, a table — practise doing it, not reading it.
  • 74% of its questions are set out as code, pseudocode or a table to complete.
  • Most of its marks (79%) come in extended questions of 12+ marks — plan the answer before writing.
  • Its biggest question so far: 30 marks (May/Jun 2025 · 9618/41 · Q3).
  • Inside the topic, §19.1 Algorithms carries the most marks (74%) and §19.2 Recursion the least (8%).
  • It is examined mostly as AO3 (Design, program & evaluate, 68%) — you must build it — write the pseudocode or program, design the structure.
  • The examiner has commented on 52 of its questions — read “What the examiner said” before you practise.

Command words

Share of questions using the word (a question can use several). What each wants →

Question shapes

  • ≤ 6 mk11
  • 7–9 mk21
  • 10–12 mk11
  • 13–15 mk4
  • 16+ mk43

Average 15.2 marks a question · 20% with a figure or table · 74% with code · biggest 30 marks

Assessment objectives — how it is examined

Every part of every current-syllabus question filed under Cambridge's AO1 / AO2 / AO3 (from its command word and what it asks you to do), so you know whether this topic pays for definitions, for applying, or for judging and building.

  • AO1 Knowledge & understanding
  • AO2 Apply & analyse
  • AO3 Design, program & evaluate

Paper 4 as a whole

Paper 4SyllabusMeasured
AO1 Knowledge & understanding0%0%
AO2 Apply & analyse0%1%
AO3 Design, program & evaluate100%99%

Syllabus = Cambridge's grid; measured = the bank's current-syllabus papers.

Inside the topic — every syllabus bullet, measured

Each part of each question is filed under the bullet it examines; the numbers are per Paper 4 sitting, exactly like the topic's. Open a bullet for its own Statometer.

  • 19.1Algorithms#2 of 7 on Paper 4Banker · 9177% next paper17.4 marks25/32 sittings May/Jun 2026

    Asked in nearly every paper — the bullet to know cold.Syllabus: linear and binary search (conditions, performance); insertion and bubble sort; finding, inserting and deleting items in stacks, queues, linked lists and binary trees; graphs as ADTs

    Next Paper 4
    77%
    8 in 10
    Marks a paper
    17.4
    23% of the paper · 74% of the topic
    Asked in
    25 / 32
    Paper 4 sittings · 81 questions
    Last asked
    May/Jun 2026
    9618/43 · Q3 · 6 marks · 5-series streak
    • Asked in 25 of 32 Paper 4 sittings — about 8 papers in 10.
    • About 17.4 marks a paper (23% of Paper 4; 74% of the topic's marks across its 3 bullets).
    • Last asked May/Jun 2026 · 9618/43 · Q3 (6 marks) — in the most recent series.
    • Asked in each of the last 5 series.
    • Rising: 13.2 → 21.6 marks a paper.
    • Usually “Write” or “Complete”: you must produce something — code, a diagram, a table — practise doing it, not reading it.
    • Biggest chunk of marks so far: 28 in May/Jun 2025 · 9618/43 · Q3.
    • It is examined mostly as AO3 (Design, program & evaluate, 62%), the rest AO2 (23%) — you must build it — write the pseudocode or program, design the structure.
    Last 12 sittings Rising
    Oct/Nov 24 · 41: 13 marksOct/Nov 24 · 42: 26 marksOct/Nov 24 · 43: 13 marksMay/Jun 25 · 41: 29 marksMay/Jun 25 · 42: 0 marksMay/Jun 25 · 43: 44 marksOct/Nov 25 · 41: 19 marksOct/Nov 25 · 42: 15 marksOct/Nov 25 · 43: 3 marksMay/Jun 26 · 41: 24 marksMay/Jun 26 · 42: 41 marksMay/Jun 26 · 43: 17 marks

    Assessment objectives

    • AO1 Knowledge & understanding
    • AO2 Apply & analyse
    • AO3 Design, program & evaluate
    • Write69%
    • Complete33%
    • Describe19%
    • Explain19%
  • 19.1Building ADTs from other ADTs#4 of 7 on Paper 4Occasional · 3328% next paper3.8 marks8/32 sittings May/Jun 2026

    Rotated in occasionally — the bullet students skip and then meet.Syllabus: stack, queue, linked list, dictionary and binary tree implementations; comparing algorithms with Big O time and space complexity

    Next Paper 4
    28%
    1 in 4
    Marks a paper
    3.8
    5% of the paper · 17% of the topic
    Asked in
    8 / 32
    Paper 4 sittings · 34 questions
    Last asked
    May/Jun 2026
    9618/43 · Q3 · 14 marks
    • Asked in 8 of 32 Paper 4 sittings — roughly one paper in 4.
    • About 3.8 marks a paper (5% of Paper 4; 17% of the topic's marks across its 3 bullets).
    • Last asked May/Jun 2026 · 9618/43 · Q3 (14 marks) — in the most recent series.
    • Easing: 4.6 → 4.1 marks a paper.
    • Usually “Write” or “Complete”: you must produce something — code, a diagram, a table — practise doing it, not reading it.
    • Biggest chunk of marks so far: 26 in May/Jun 2024 · 9618/42 · Q2.
    • It is examined mostly as AO3 (Design, program & evaluate, 70%) — you must build it — write the pseudocode or program, design the structure.
    Last 12 sittings Easing
    Oct/Nov 24 · 41: 0 marksOct/Nov 24 · 42: 0 marksOct/Nov 24 · 43: 0 marksMay/Jun 25 · 41: 19 marksMay/Jun 25 · 42: 0 marksMay/Jun 25 · 43: 1 marksOct/Nov 25 · 41: 0 marksOct/Nov 25 · 42: 0 marksOct/Nov 25 · 43: 0 marksMay/Jun 26 · 41: 0 marksMay/Jun 26 · 42: 0 marksMay/Jun 26 · 43: 14 marks

    Assessment objectives

    • AO1 Knowledge & understanding
    • AO2 Apply & analyse
    • AO3 Design, program & evaluate
    • Write59%
    • Complete59%
    • State29%
    • Explain27%
  • 19.2Recursion#5 of 7 on Paper 4Occasional · 2028% next paper1.5 marks8/32 sittings Oct/Nov 2025

    Rotated in occasionally — the bullet students skip and then meet.Syllabus: essential features; writing and tracing recursive algorithms; when recursion is beneficial; how a compiler uses the stack (winding and unwinding)

    Next Paper 4
    28%
    1 in 4
    Marks a paper
    1.5
    2% of the paper · 8% of the topic
    Asked in
    8 / 32
    Paper 4 sittings · 19 questions
    Last asked
    Oct/Nov 2025
    9618/43 · Q3 · 6 marks · 1 series ago
    • Asked in 8 of 32 Paper 4 sittings — roughly one paper in 4.
    • About 1.5 marks a paper (2% of Paper 4; 8% of the topic's marks across its 3 bullets).
    • Last asked Oct/Nov 2025 · 9618/43 · Q3 (6 marks), 1 series ago.
    • Usually “Write” or “Complete”: you must produce something — code, a diagram, a table — practise doing it, not reading it.
    • Biggest chunk of marks so far: 14 in May/Jun 2024 · 9618/42 · Q3.
    • It is examined mostly as AO3 (Design, program & evaluate, 54%), the rest AO1 (31%) — you must build it — write the pseudocode or program, design the structure.
    Last 12 sittings Steady
    Oct/Nov 24 · 41: 0 marksOct/Nov 24 · 42: 0 marksOct/Nov 24 · 43: 0 marksMay/Jun 25 · 41: 0 marksMay/Jun 25 · 42: 0 marksMay/Jun 25 · 43: 0 marksOct/Nov 25 · 41: 0 marksOct/Nov 25 · 42: 3 marksOct/Nov 25 · 43: 6 marksMay/Jun 26 · 41: 0 marksMay/Jun 26 · 42: 0 marksMay/Jun 26 · 43: 0 marks

    Assessment objectives

    • AO1 Knowledge & understanding
    • AO2 Apply & analyse
    • AO3 Design, program & evaluate
    • Write58%
    • Complete47%
    • Explain26%
    • Describe21%

19% of the topic's marks sit in question parts that belong to another topic (scenario questions cross sections) or that no bullet claims; they count for the topic, not for a bullet.

Marks a paper, year by year

212223242526

By exam series

  • May/Jun14/18 · 32.2 mk
  • Oct/Nov14/14 · 25.9 mk

Across papers: Paper 3 100% of sittings, ~13.3 marks · Paper 4 88% of sittings, ~29.4 marks.

What you need to know3syllabus §19.1, §19.2

  1. 19.1Algorithmslinear and binary search (conditions, performance); insertion and bubble sort; finding, inserting and deleting items in stacks, queues, linked lists and binary trees; graphs as ADTs
  2. 19.1Building ADTs from other ADTsstack, queue, linked list, dictionary and binary tree implementations; comparing algorithms with Big O time and space complexity
  3. 19.2Recursionessential features; writing and tracing recursive algorithms; when recursion is beneficial; how a compiler uses the stack (winding and unwinding)

Video lectures33ZAK's YouTube channel · play here

  • A29618 Paper 320241.3K views

  • A22024522 views

  • ASA22024642 views

  • ASA22024293 views

  • ASA22024291 views

  • A22023422 views

Infographics5draw these the way the examiner expects · download as PNG

Stack · Queue · Linked listKnow the pointers each one needs and what happens on add/remove.STACK (LIFO)739← TopPointerBasePointerPUSH adds at top, POP removes from top.Check full (overflow), empty (underflow).Used for: recursion, interrupts, undo.QUEUE (FIFO)4816FrontRearEnqueue at rear, dequeue at front.Circular queue wraps around with MOD.Used for: print jobs, keyboard buffer, BFS.LINKED LIST1225320StartPointer → node 1 · each node = data + pointer to next · free list holds unused nodesInsert/delete = change pointers only (no shifting). Traversal must start from the head.cswithzak.com

Stack, queue & linked list

ASA2
Recursion: winding & unwindingA recursive routine calls itself. It MUST have a base case (stops) and a general case (moves towards thebase case).FUNCTION Fact(N : INTEGER) RETURNS INTEGER IF N <= 1 THEN RETURN 1 // base ENDIF RETURN N * Fact(N-1) // generalENDFUNCTIONCall stack during Fact(4)Fact(1) → 1Fact(2) → 2 × 1Fact(3) → 3 × 2Fact(4) → 4 × 6← top (last pushed)← bottom (first call)Winding (calls pushed)Fact(4) = 4 × Fact(3)Fact(3) = 3 × Fact(2)Fact(2) = 2 × Fact(1)Fact(1) = 1 ← base caseUnwinding (results returned)Fact(2) = 2 × 1 = 2Fact(3) = 3 × 2 = 6Fact(4) = 4 × 6 = 24Every call pushes a stack frame: parameters, local variables,return address. Nothing is multiplied until the base casereturns — then frames pop and the products build up.No base case → infinite recursion → stack overflow.Recursion vs iterationRecursion: shorter, natural for trees, quicksort, Fibonacci, Towers of Hanoi — but uses stack memory and can be slower.Iteration: a loop with explicit counters — usually faster and memory-safe. Any recursion can be rewritten as a loop with a stack.cswithzak.com

Recursion: winding & unwinding

A2
Big O — how running time grows with nBig O describes the worst-case growth as the input size n gets large; constants and smaller terms areignored.input size n →timeO(1)O(log n)O(n)O(n log n)O(n²)O(2ⁿ)AlgorithmBig Ohash table lookup, array indexO(1)binary searchO(log n)linear search, traversalO(n)merge sort, quicksort (avg)O(n log n)bubble / insertion sortO(n²)brute-force subsetsO(2ⁿ)How to read code: a single loop over n → O(n);a loop inside a loop → O(n²); halving each step→ O(log n); constant work → O(1).Space complexity works the same way for memory.Why it mattersn = 1 000 000: O(log n) ≈ 20 steps, O(n) = 1 000 000, O(n²) = 10¹² — the difference between instant and days.Choosing a sorted array + binary search, or a hash table, is often the whole optimisation.Best / average / worst case can differ (quicksort worst O(n²)); Big O quotes the worst unless told otherwise.Drop constants and lower terms: 3n² + 10n + 7 → O(n²). Nested loops multiply; sequential loops add (and the bigger wins).cswithzak.com

Big O & algorithm performance

A2
Binary search tree, hash table & dictionaryA node = data + left pointer + right pointer. In a BST smaller keys go left, larger go right — soin-order traversal is sorted.50307020406080rootInsert 45: 45 < 50 → left; 45 > 30 → right; 45 > 40 → right → new right child of 40.In-order (left, node, right): 20 30 40 50 60 70 80 — sorted. Pre-order: 50 30 20 40 70 60 80.Post-order: 20 40 30 60 80 70 50. Search is O(log n) if balanced, O(n) if it degenerates into a list.Array implementationidxDataLeftRight150232304537067420005400066000780000 = null pointer · Root = 1 · free-list pointer for empty slotsHash table / dictionarykey → hash function → index"Ali" → 2, "Sara" → 5 …lookup / insert ≈ O(1)collision: two keys, one index→ linear probe, chaining→ rehash when nearly fullDictionary = key:value ADT,usually built on a hash table.Python dict, Java HashMap,VB.NET Dictionary(Of K, V).Traversals in pseudocodePROCEDURE InOrder(P : INTEGER) IF P <> 0 THEN CALL InOrder(Tree[P].Left) // recurse left OUTPUT Tree[P].Data CALL InOrder(Tree[P].Right) // recurse right ENDIFENDPROCEDUREMove the OUTPUT line to the top forpre-order, to the bottom for post-order.Pre-order copies a tree; post-order deletesone safely (children before parent); in-ordergives sorted output from a BST.cswithzak.com

Binary tree & hash table

A2

Browse all infographics →

Key terms12use these exact words in the exam

recursionbase casestack framebinary treehash tabledictionarycollisionBig OO(1)O(n)O(log n)O(n²)

Dotted terms are defined in the glossary.

Code help3referenced to the Cambridge pseudocode guide

Recursion: factorial, sum of digits, Fibonacci

pseudocode §8.2 Run in Playground
FUNCTION Factorial(N : INTEGER) RETURNS INTEGER
IF N <= 1 THEN
RETURN 1
ENDIF
RETURN N * Factorial(N - 1)
ENDFUNCTION
FUNCTION SumDigits(N : INTEGER) RETURNS INTEGER
IF N < 10 THEN
RETURN N
ENDIF
RETURN (N MOD 10) + SumDigits(N DIV 10)
ENDFUNCTION
FUNCTION Fib(N : INTEGER) RETURNS INTEGER
IF N <= 2 THEN
RETURN 1
ENDIF
RETURN Fib(N - 1) + Fib(N - 2)
ENDFUNCTION
OUTPUT Factorial(5), " ", SumDigits(345), " ", Fib(10)

Binary search — recursive

pseudocode Run in Playground
DECLARE A : ARRAY[1:8] OF INTEGER
DECLARE i : INTEGER
FOR i 1 TO 8
A[i] i * i
NEXT i
FUNCTION BSearch(Target : INTEGER, Low : INTEGER, High : INTEGER) RETURNS INTEGER
DECLARE Mid : INTEGER
IF Low > High THEN
RETURN -1
ENDIF
Mid (Low + High) DIV 2
IF A[Mid] = Target THEN
RETURN Mid
ENDIF
IF A[Mid] < Target THEN
RETURN BSearch(Target, Mid + 1, High)
ENDIF
RETURN BSearch(Target, Low, Mid - 1)
ENDFUNCTION
OUTPUT "49 is at index ", BSearch(49, 1, 8)

Binary tree with arrays: insert + in-order traversal

pseudocode Run in Playground
DECLARE Val, L, R : ARRAY[1:10] OF INTEGER
DECLARE Root, Free, i : INTEGER
Root 0
Free 1
FOR i 1 TO 10
L[i] 0
R[i] 0
NEXT i
PROCEDURE Insert(V : INTEGER)
DECLARE P : INTEGER
Val[Free] V
IF Root = 0 THEN
Root Free
ELSE
P Root
REPEAT
IF V < Val[P] THEN
IF L[P] = 0 THEN
L[P] Free
P 0
ELSE
P L[P]
ENDIF
ELSE
IF R[P] = 0 THEN
R[P] Free
P 0
ELSE
P R[P]
ENDIF
ENDIF
UNTIL P = 0
ENDIF
Free Free + 1
ENDPROCEDURE
PROCEDURE InOrder(P : INTEGER)
IF P <> 0 THEN
CALL InOrder(L[P])
OUTPUT Val[P]
CALL InOrder(R[P])
ENDIF
ENDPROCEDURE
CALL Insert(50)
CALL Insert(30)
CALL Insert(70)
CALL Insert(20)
CALL Insert(40)
CALL InOrder(Root)

Playground examples33runnable programs for this topic

  • Binary search (iterative)

    Only works on sorted data: halve the search range each time by comparing with the middle element.

    ASA2PseudocodeSearching 9618 §19.1
    Run
  • Binary search (recursive)

    Two base cases — empty range (not found) and middle matches (found) — and two recursive calls.

    A2PseudocodeSearching 9618 §19.1
    Run
  • Linear vs binary search — count the comparisons

    Big O in practice: 1000 sorted items, linear search is O(n) and binary search O(log n).

    A2PseudocodeSearching 9618 §19.1
    Run
  • Bubble sort with a Swapped flag

    Stop early when a whole pass makes no swaps — the efficient version examiners like.

    ASA2PseudocodeSorting 9618 §9.2, §19.1
    Run
  • Insertion sort

    Take each value in turn and slide it left into the sorted part of the array.

    ASA2PseudocodeSorting 9618 §19.1
    Run
  • Stack — push, pop, overflow & underflow

    LIFO with an array and a Top pointer. Watch what happens on the 4th pop.

    ASA2PseudocodeStacks, queues, lists & trees 9618 §10.4, §19.1
    Run
  • Linear queue — enqueue & dequeue

    FIFO with Front and Rear pointers. Notice the space at the front is never reused.

    ASA2PseudocodeStacks, queues, lists & trees 9618 §10.4, §19.1
    Run
  • Circular queue

    Rear MOD Size + 1 wraps the pointer back to slot 1 so freed space is reused.

    A2PseudocodeStacks, queues, lists & trees 9618 §19.1
    Run
  • Linked list in arrays with a free list

    Insert at the front, delete by value, traverse — using Data/Pointer records, StartPointer and FreePointer.

    ASA2PseudocodeStacks, queues, lists & trees 9618 §10.4, §19.1
    Run
  • Binary search tree — insert, search & three traversals

    Nodes in an array with LeftPointer/RightPointer; in-order traversal outputs the values sorted.

    A2PseudocodeStacks, queues, lists & trees 9618 §19.1
    Run
  • Hash table with linear probing

    Key MOD TableSize gives the slot; on a collision, step to the next free slot.

    A2PseudocodeStacks, queues, lists & trees 9618 §19.1
    Run
  • Dictionary — key → value pairs

    A dictionary ADT built from an array of records: Add updates an existing key or appends a new one; Lookup returns the value.

    A2PseudocodeStacks, queues, lists & trees 9618 §19.1
    Run
  • Using a stack — balanced brackets

    Push every ( and pop on every ); the expression is balanced if the stack ends empty and never underflows.

    A2PseudocodeStacks, queues, lists & trees 9618 §19.1
    Run
  • Evaluate Reverse Polish Notation with a stack

    Operands are pushed; an operator pops two, applies itself and pushes the result — how a compiler evaluates RPN.

    A2PseudocodeStacks, queues, lists & trees 9618 §16.3
    Run
  • Factorial — the first recursive function

    Base case N <= 1 returns 1; the general case calls itself with N - 1.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • Winding and unwinding — watch the call stack

    Output before the recursive call happens on the way down; output after it happens on the way back up.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • Fibonacci numbers

    Two base cases and two recursive calls — simple to write, expensive to run.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • Recursive power & digit sum

    Two more classic recursive definitions: x^n = x * x^(n-1), and digit sum = last digit + digit sum of the rest.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • Greatest common divisor (Euclid)

    GCD(A, B) = GCD(B, A MOD B) until B is 0.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • Tower of Hanoi

    Move N-1 discs out of the way, move the biggest, move the N-1 back. 2^N - 1 moves.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • Recursion vs iteration

    The same sum written both ways — same answer, different use of the stack.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • Reverse a string recursively

    Reverse of S = reverse of everything after the first character, followed by the first character.

    A2PseudocodeRecursion 9618 §19.2
    Run
  • A Stack class

    The stack ADT wrapped in a class: the array and Top are PRIVATE, so only Push/Pop can touch them.

    A2PseudocodeObject-oriented programming §10.1 · 9618 §19.1
    Run
  • Frequency count with a list and a dictionary

    Count dice rolls two ways: a list indexed by value, and a dictionary (the ADT the exam calls a dictionary).

    ASA2PythonArrays §3 ↔ Python
    Run
  • Binary search — iterative and recursive

    Halve the search space each time; the list must be sorted. Both versions, with the number of comparisons.

    ASA2PythonSearching §7 ↔ Python
    Run
  • Insertion sort

    Take each item and slide it left into the sorted part. Compare with sorted() and .sort() at the end.

    ASA2PythonSorting §7 ↔ Python
    Run
  • Stack and queue with a list

    append/pop give a stack; append/pop(0) a queue. Watch the order things come out.

    ASA2PythonStacks, queues, lists & trees §10 ADT ↔ Python
    Run
  • Stack as a class with an array and a pointer

    The exam implementation: fixed-size list, top pointer, isFull / isEmpty checks — not just list.pop().

    A2PythonStacks, queues, lists & trees 9618 §19.1 ↔ Python
    Run
  • Linked list with nodes and pointers

    Node objects linked by a next reference; insert at the front, traverse, delete a value.

    A2PythonStacks, queues, lists & trees 9618 §19.1 ↔ Python
    Run
  • Binary search tree — insert and in-order traversal

    Recursive insert and traversal on a tree of nodes. In-order gives the values sorted.

    A2PythonStacks, queues, lists & trees 9618 §19.1 ↔ Python
    Run
  • Hash table with linear probing

    A hash function (key mod size) and collision handling by probing — the §19.1 implementation, before using dict.

    A2PythonStacks, queues, lists & trees 9618 §19.1 ↔ Python
    Run
  • Recursion — factorial, Fibonacci, GCD

    Base case + general case. Open the Trace tab to see n shrink as the calls wind in.

    A2PythonRecursion 9618 §19.2 ↔ Python
    Run
  • Towers of Hanoi

    The classic recursive puzzle: move n-1 discs aside, move the last, move the n-1 back.

    A2PythonRecursion 9618 §19.2 ↔ Python
    Run

Declarative Lab4Prolog knowledge bases with sample queries

  • ancestor/2 — the classic recursive rule

    Base case: a parent is an ancestor. Recursive case: a parent of an ancestor is an ancestor. Order of the clauses matters.

    A2Recursive rules 9618 §20.1
    Run
  • Lists — [Head | Tail]

    A list is a head and a tail. Unification splits it: [H|T] = [a, b, c] gives H = a, T = [b, c].

    A2Lists 9618 §20.1
    Run
  • Recursing over a list — length, sum, count

    Base case on [], recursive case on [H|T]. Note that the arithmetic comes AFTER the recursive call so N1 has a value.

    A2Lists 9618 §20.1
    Run
  • Factorial and Fibonacci

    Recursion with arithmetic. The guard N > 0 stops the recursion running below zero.

    A2Arithmetic 9618 §20.1
    Run

Data Structures Playground16operation scripts on stacks, queues, linked lists, trees and hash tables — stepped through with the pseudocode

  • Push, push, pop

    The first stack question: three pushes, two pops, what is left and where is TopPointer?

    ASA2Stacks 9618 §10.4
    Open
  • Stack overflow and underflow

    A stack of 3: the fourth push is refused; popping past empty is refused too.

    ASA2Stacks 9618 §10.4
    Open
  • Stack as a bracket checker

    Push every opening bracket, pop on every closing one — the stack is empty at the end only if the brackets match.

    A2Stacks 9618 §19.1
    Open
  • Linear queue fills up

    Why a linear queue is wasteful: after dequeuing, the space at the front cannot be reused.

    ASA2Queues 9618 §10.4
    Open
  • Circular queue wrap-around

    The same operations on a circular queue: RearPointer wraps to 1 and Ed fits.

    ASA2Queues 9618 §10.4
    Open
  • Print queue

    Jobs arrive and are printed in order — FIFO in action.

    ASQueues 9618 §10.4
    Open
  • Insert into an ordered linked list

    Values arrive out of order; each takes the next free node and is linked in order — watch StartPointer and FreePointer.

    ASA2Linked lists 9618 §10.4
    Open
  • Delete from a linked list

    Deleting relinks the previous node and returns the node to the free list — the data may still be visible in the array.

    A2Linked lists 9618 §19.1
    Open
  • Search a linked list

    Follow the pointers from StartPointer until the value or pointer 0.

    A2Linked lists 9618 §19.1
    Open
  • Build a binary search tree

    Insert 50, 30, 70, 20, 40, 60, 80 — smaller to the left, larger to the right — and read off the arrays.

    A2Binary trees 9618 §19.1
    Open
  • In-order, pre-order, post-order

    The same tree, three traversals — in-order gives sorted output; the others are the exam's favourite trick.

    A2Binary trees 9618 §19.1
    Open
  • Tree of words

    Strings compare alphabetically — insert names and search for one.

    A2Binary trees 9618 §19.1
    Open
  • Sorted input makes a bad tree

    Inserting 1, 2, 3, 4, 5 in order gives a tree that is really a list — every FIND walks the whole thing.

    A2Binary trees 9618 §19.1
    Open
  • Hash table with MOD 10

    Keys hashed with key MOD 10; 23 and 13 collide, so 13 is probed to the next free slot.

    A2Dictionaries / hash tables 9618 §19.1
    Open
  • Hashing words by ASCII sum

    A string key is turned into a number (sum of ASCII codes) before MOD — see the working for each key.

    A2Dictionaries / hash tables 9618 §19.1
    Open
  • Delete leaves a tombstone

    Deleting a key that another key was probed past must not break later lookups.

    A2Dictionaries / hash tables 9618 §19.1
    Open

OOP Designer1class diagrams with the skeleton code in four languages

  • Read: a Stack class

    A class that wraps an array and TopPointer — the ADT as an object.

    A2Read the code → draw the diagram 9618 §20.1
    Open

Test yourself

Ready to check you know it?

Every round is a fresh random draw, weak cards come back until you get them right, and past-paper questions come with their mark schemes. Marks earn XP on your dashboard.

Enroll nowOnline classes