CS
Topic 7 Algorithm design & problem-solving · Page 7.7–7.10

Algorithms & Debugging

You've designed programs. Now watch them run, trace their state, work out what they do, and fix them when they break. Standard methods, trace tables, the purpose of an algorithm, and finding and correcting errors — four skills Paper 2 regularly tests.

🧪 Exam Mode ON — recall from memory, then reveal

📚 Book Notes — start here

Read this before you tap the activities. These notes give you everything you need to feel confident before the Trace Table Runner and Bug Hunter. When you're ready, tap 🎓 Learn.

🎯 Topic Overview

In the Development Lifecycle lab you designed programs. Now you'll run them. That means recognising the standard methods in the syllabus (linear search, bubble sort, totalling, counting, and finding the maximum, minimum and average), tracing an algorithm step by step in a trace table, stating an algorithm's purpose and describing its processes, and finding and correcting errors. Trace-table questions appear regularly on Paper 2 — careful tracing is hard to bluff.

🎓 Learning Objectives

By the end of this lab you should be able to…

  • Recognise and write the standard methods: linear search, bubble sort, totalling, counting, and finding the maximum, minimum and average
  • Use counter and totaller (accumulator) variables correctly
  • Complete a trace table, recording every variable change, output and user prompt
  • Follow nested loops without losing track of the outer variables
  • State the purpose of an algorithm, and describe the processes it uses
  • Identify errors in an algorithm and suggest corrections

📖 Key Terminology

TermOne-line definition
AlgorithmA sequence of steps to solve a problem.
Trace tableA table used to step through an algorithm and record every variable value.
Linear searchCheck each element in turn until the target is found or the list ends.
Bubble sortCompare adjacent pairs; swap if out of order; repeat until no swaps.
CountingUsing a counter variable to count how many times something occurs (e.g. how many pass a test).
TotallingUsing a totaller variable to keep a running total (also called an accumulator).
IterationRepeating steps in a loop; one pass of a loop is also called an iteration.
Nested loopA loop inside another loop (bubble sort's core pattern).
FlagA BOOLEAN variable that signals "done" or "found" — often used to exit a loop early.
Syntax error (useful background)Code that breaks the rules of the language (missing ENDIF, wrong keyword).
Logic errorCode that runs but produces the wrong result (> instead of >=).
Runtime errorCode that fails while running (dividing by zero, array out of bounds).

🧠 Core Theory

The standard methods (syllabus)

MethodWhat it doesCore pattern
Linear searchCheck each element in turn; stop when foundLoop + IF + flag
Bubble sortRepeatedly compare and swap adjacent pairsNested loop + swap
TotallingKeep a running totalTotal ← Total + Value
CountingCount how many items matchCount ← Count + 1
MaxTrack the largest value seenLoop + IF (>)
MinTrack the smallest value seenLoop + IF (<)
AverageSum + count then divideTotaller ÷ counter

Three types of programming error (useful background — the syllabus asks you to identify errors and suggest corrections)

TypeMeaningExample
SyntaxBreaks the language rulesMissing ENDIF; NEXT j when the loop was FOR i
LogicCode runs but gives wrong resultUsing > when you meant >=; outputting the wrong variable
RuntimeFails during executionDividing by zero; accessing Array[11] in a 10-element array

Trace table rules (the ones examiners flag)

✎ Record every variable assignment — never skip.
✎ Record outputs and user prompts (e.g. Enter a score) in the Output column.
✎ No quotation marks around output values in the Output column.
✎ Once the algorithm ends (loop exits), stop tracing. No further rows.
✎ Trace all given input data — not just the first value.
✎ Leave a cell blank if that variable didn't change on that step.

💡 Worked Examples

Example 1 · Trace the Max algorithm

Given Numbers = [23, 41, 17, 88, 5] and this algorithm:

Max ← Numbers[1]
FOR i ← 2 TO 5
    IF Numbers[i] > Max
      THEN
        Max ← Numbers[i]
    ENDIF
NEXT i
OUTPUT Max

The trace table:

iNumbers[i]MaxOutput
23
24141
317
48888
55
88

Note: no quotation marks on 88. Blank cells where variables don't change.

Example 2 · What does this algorithm do?

Count ← 0
FOR i ← 1 TO 30
    INPUT Score
    IF Score >= 50
      THEN
        Count ← Count + 1
    ENDIF
NEXT i
OUTPUT Count

State the purpose (one sentence): It counts how many of 30 input scores are 50 or greater (i.e. how many passed).

Describe the processes (the steps): Count is set to 0; a loop runs 30 times; each time a score is input and, if it is 50 or more, 1 is added to Count; after the loop Count is output.

Notice the pattern: Count is a counter. The IF is a filter. Loop 30 times = process 30 items.

⚠️ Common Misconceptions

❌ Putting quotation marks around trace outputs

The trace table Output column stores values, not strings-as-text. Write Found at 3, not "Found at 3". Examiners deduct for the quotes.

❌ Continuing the trace past the loop exit

Once the algorithm's stopping condition is met, no further rows go in the trace table. A WHILE that exits at i=6 doesn't get a row for i=7.

❌ Answering "state the purpose" with a line-by-line walkthrough

"It sets Count to 0, then loops from 1 to 30…" is reading the code back, not stating its purpose. Say what it does: "It counts how many scores are ≥ 50." But if the question says describe the processes, then you do explain the main steps.

❌ Bubble sort with only one loop

Bubble sort needs a nested loop: outer for passes, inner for comparisons. A single loop sorts only one pair, not the whole list.

❌ Skipping a variable in the trace when it doesn't change

Leave the cell blank — but don't remove the row. Every iteration gets a row; unchanged variables just don't get a value.

📋 Cambridge Exam Focus

Mark-winning tips distilled from the 2023–2025 examiner reports:

✎ Record every assignment in the trace table — never skip a variable change. Include outputs and user prompts.
✎ No quotes on outputs — the Output column stores values, not string literals.
✎ Stop when the algorithm stops — no rows after the loop exit condition is met.
✎ Trace all the given input data, not just the first value.
✎ State the purpose = one sentence on what it does overall. Describe the processes = the main steps.
✎ Read the code twice when hunting errors: pass 1 for keywords and structure, pass 2 for logic. Give the line and a correction.
✎ Bubble sort needs repeated passes — nested loops, or a loop with a flag that stops when a pass makes no swaps.

🎯 Quick Knowledge Check

Tap each question to reveal the answer. Aim for at least 5 of 6.

1. What are the standard methods listed in the syllabus?
Linear search, bubble sort, totalling, counting, and finding the maximum, minimum and average.
2. What's the difference between a syntax error and a logic error?
Syntax = breaks the language rules (won't run). Logic = runs but gives the wrong result.
3. Should trace-table outputs be wrapped in quotation marks?
No — never. The Output column stores values, not string literals.
4. What does a counter do?
It counts how many times something occurs (starts at 0, increments by 1 each time).
5. Why does bubble sort need repeated passes?
One pass only moves the largest value to the end. The outer loop (or a flag loop) repeats the passes; the inner loop compares adjacent pairs within each pass.
6. "State the purpose" vs "describe the processes" — what's the difference?
State the purpose: one sentence on what it does overall. Describe the processes: explain the main steps it uses.

✅ Ready for activities when you can…

Name the standard methods. Follow every trace-table rule (record every assignment, outputs and prompts, no quotes on outputs, stop at loop exit). State an algorithm's purpose in one sentence, and describe its processes when asked.

Ready? Tap the 🎓 Learn tab and try the Trace Table Runner — step through three real algorithms and watch the variables update live.

📚 From the Textbook

The syllabus lists a small set of standard methods: linear search (check each item until found), bubble sort (repeatedly compare and swap adjacent pairs), totalling, counting, and finding the maximum, minimum and average. All of them can be tested by tracing — stepping through the code with sample data and recording every variable, output and prompt in a table. If you can trace it, you understand it. Trace tables also help you work out the purpose of an unfamiliar algorithm and find the errors in a faulty one. Get comfortable with tracing and this whole section opens up.

💡 Getting Started

Grab a pen and paper. Copy the Max algorithm from Book Notes. Pick your own array of 5 numbers. Draw a trace table with columns for i, Numbers[i], Max, Output. Step through by hand — one row per iteration. If your Max ends as the largest number in your list, you've done it right. That's the skill trace-table questions test.

🔬 Computer Science in Context

Debuggers in real IDEs (VS Code, PyCharm, Visual Studio) are essentially live trace tables. You set a breakpoint, run the program, and the IDE shows you every variable's current value, stepping through one instruction at a time. Learning trace tables is learning how to think like a debugger — a skill you'll use every day as a professional programmer.

💬 Discussion

Bubble sort is famously inefficient on large data (O(n²)) — modern languages use quicksort or Timsort instead. So why does Cambridge still teach bubble sort? What is the educational value of an "inefficient" algorithm?

⚠️ Three kinds of error (useful background)

Exam questions ask you to identify the error (usually by line number) and suggest a correction. Knowing the type of error helps you find it, but naming the type is not required by the syllabus.

🔧 Syntax

What: code that breaks the language rules.
When: found by the translator — a compiler reports it before the program runs; an interpreter stops when it reaches that line.
Examples: missing ENDIF, NEXT j when the loop is FOR i, misspelled keyword.

🎯 Logic

What: code that runs but produces the wrong answer.
When: only visible when you test with data.
Examples: > where you meant >=, outputting the wrong variable, off-by-one in a loop range.

💥 Runtime

What: code that fails while running.
When: program crashes mid-execution.
Examples: dividing by zero, accessing Array[11] in a 10-element array, reading a file that doesn't exist.

📊 Trace Table Runner — step through algorithms live

Pick an algorithm, click Step, watch the current line highlight and the trace table build up. Values from the same pass share a row; a cell stays blank when nothing changes. This is the skill trace-table questions test.

Pseudocode
Trace table
Click Step to begin. The highlighted line shows the instruction that just ran; the trace table row shows what changed.

🐛 Bug Hunter — spot the errors

The program below is meant to find the highest of 5 scores (all 0 or more). It has 3 errors. Click any line you think has an error. You get instant feedback and the correction. Try to find all 3.
🎯 Bugs found: 0 / 3
Click any line to make a guess.

📝 Standard methods — reference

The standard methods in the syllabus, in the syllabus pseudocode style. Learn to spot them at a glance. (N stands for the number of items.)

🔍 Linear searchFound ← FALSE i ← 1 WHILE Found = FALSE AND i <= N DO IF List[i] = Target THEN Found ← TRUE OUTPUT "Found at ", i ENDIF i ← i + 1 ENDWHILE
🫧 Bubble sort (nested loops)FOR i ← 1 TO N - 1 FOR j ← 1 TO N - i IF List[j] > List[j + 1] THEN Temp ← List[j] List[j] ← List[j + 1] List[j + 1] ← Temp ENDIF NEXT j NEXT i
🫧 Bubble sort with a flag (stops early)Last ← N - 1 REPEAT Swapped ← FALSE FOR j ← 1 TO Last IF List[j] > List[j + 1] THEN Temp ← List[j] List[j] ← List[j + 1] List[j + 1] ← Temp Swapped ← TRUE ENDIF NEXT j Last ← Last - 1 UNTIL Swapped = FALSE OR Last = 0
➕ TotallingTotal ← 0 FOR i ← 1 TO N INPUT Value Total ← Total + Value NEXT i OUTPUT Total
🧮 CountingCount ← 0 FOR i ← 1 TO N IF List[i] >= Threshold THEN Count ← Count + 1 ENDIF NEXT i OUTPUT Count
📊 Average (total ÷ number of values)Total ← 0 FOR i ← 1 TO N INPUT Value Total ← Total + Value NEXT i Average ← Total / N OUTPUT Average
🔺 MaximumMax ← List[1] FOR i ← 2 TO N IF List[i] > Max THEN Max ← List[i] ENDIF NEXT i OUTPUT Max
🔻 MinimumMin ← List[1] FOR i ← 2 TO N IF List[i] < Min THEN Min ← List[i] ENDIF NEXT i OUTPUT Min

🎯 Six mark-scheme traps (from the examiner reports)

Trap 1 · Quotation marks on trace outputs

The Output column stores values, not strings. Write Found at 3, not "Found at 3". Examiner reports flag the quotes.

Cited: 2025 s25 · 2024 w24 — identical wording in both reports

Trap 2 · Continuing the trace past the loop exit

Once the stopping condition is met, no further rows go in the trace table. A WHILE that exits at i=6 doesn't get a row for i=7.

Cited: 2025 s25 · 2024 w24

Trap 3 · Not recording every variable assignment

"When tracing an algorithm, each time a variable has a value assigned to it, this needs to be recorded in the trace table." — Cambridge examiner report 2024 w24.

Cited: 2024 w24 examiner report

Trap 4 · A walkthrough when asked to STATE the purpose

"State the purpose" wants the overall goal in one sentence — "It counts how many scores pass" — not a walkthrough of every line. (If the question says "describe the processes", then explain the main steps.)

Cited: 2023 w23 Q4(b), 2024 s24 Q3(b)

Trap 5 · Bubble sort with only one loop

"Some attempts at the bubble sort only used one loop" — 2024 w24. One pass is not enough: use an outer loop (or a flag loop) for the passes and an inner loop for the comparisons.

Cited: 2024 w24 Q12

Trap 6 · Identifying the same error twice (Yes/No swap on a decision)

"Many candidates identified the same error twice with the switching of the Yes and No on one of the decision boxes." When correcting errors, count the DISTINCT bugs, not the same bug from two angles.

Cited: 2024 w24 examiner report
Path

Topic 7.7–7.10 Learning Journey

Mistakes

Mistakes log

Progress

Stats & graphs

0%Accuracy
0Answered
0Streak
Bookmarks

Bookmarks

My Notes

My Notes

Title
Note
Knowledge Vault 2.0

Knowledge Vault

Add entry

Category
Confidence
Title
Info

Entries

🎮 Drill 1 — Algorithm Identifier

A pseudocode snippet is shown. Which standard method is it?

🎮 Drill 2 — Trace Row Predictor

Given the current state of a trace and the next instruction, what value comes next?

🎮 Drill 3 — Error Type Sorter

An error is described. Is it a syntax, logic, or runtime error? (Useful background — it helps you find errors.)

🎮 Drill 4 — Purpose Finder

A short algorithm is shown. Pick the one-sentence statement of its purpose.

🎮 Drill 5 — 60-Second Algorithm Sprint

Rapid questions on algorithms, tracing and errors — answer as many as you can in 60 seconds.

60
0 / 0

✎ Practice questions

📋 Exam Mode — Cambridge-style questions

Cambridge-style questions modelled on Paper 2 (2023–2025) and the examiner reports — the tag shows the paper each is based on. Each question includes the algorithm you need. Type your answer, then mark it.

🔄 Traps — mark-scheme wording

Trap · Quotation marks on trace outputs

The Output column stores values, not string literals. Never wrap outputs in quotes.

2025 s25 · 2024 w24 (identical wording)

Trap · Continuing the trace past the loop exit

Once the stopping condition is met, no further rows go in the trace table.

2025 s25 · 2024 w24

Trap · Missing a variable re-assignment

Every time a variable is assigned, record it. Even if the value looks the same.

2024 w24 examiner report

Trap · Only tracing the first data item

If the question gives 5 inputs, trace all 5. Do not stop after the first.

2025 s25 Q9

Trap · A walkthrough when asked to state the purpose

State the purpose = ONE sentence on the overall goal. Describe the processes = the main steps. Read which one the question asks for.

2023 w23 Q4(b), 2024 s24 Q3(b)

Trap · Bubble sort with only one loop

One pass is not enough. Outer loop (or flag loop) for the passes, inner loop for the comparisons.

2024 w24 Q12

Trap · Identifying the same error twice (Yes/No swap)

A decision box with swapped Yes/No labels is ONE error, not two. Count distinct bugs.

2024 w24 examiner report

Trap · Adding SQL 'ASC' to a bubble sort

SQL keywords (ASC, DESC, ORDER BY) belong in database queries — not in sort pseudocode.

2024 w24 examiner report

Trap · Leaving out user prompts

The syllabus says a trace table records variables, outputs and user prompts at each step. A prompt such as Enter a score goes in the Output column (no quotes).

Syllabus 7.7

Trap · Endless loops from unreachable exit conditions

A WHILE that never reaches its terminal condition runs forever. Always check that the loop variable is modified inside the loop.

2024 w24 examiner report

🧠 Memory triggers

LB TC MMA

The standard methods.
Linear search · Bubble sort · Totalling · Counting · Maximum · Minimum · Average.

SLR

The three error types (useful background).
Syntax (breaks the language rules) · Logic (wrong answer) · Runtime (fails while running).

Trace rule of 5

The trace-table checklist.
1. Record every assignment. 2. Record outputs and prompts. 3. No quotes on outputs. 4. Stop when the algorithm stops. 5. Blank cells for unchanged variables.

Bubble sort DNA

Repeated passes + swap.
Outer loop = passes. Inner loop = adjacent comparisons. IF out of order → swap using a Temp variable. Common improvement: a flag that stops when a pass makes no swaps.

State vs Describe

Purpose vs processes.
State the purpose: "It counts how many scores pass" ✓
Describe the processes: "Count is set to 0, a loop inputs 30 scores, 1 is added to Count for each score ≥ 50, then Count is output" ✓

🏆 Skill grid

Green = secured (≥70%). Yellow = focus (30–69%). Grey = not attempted. Tap any badge to override.

🎉 Topic 7 complete

When you finish this lab, Topic 7 Algorithm Design & Problem-Solving is complete: the development life cycle, design methods, validation, verification and test data, plus the standard methods, trace tables and finding errors. Next is Topic 8: Programming.

✔ Saved