Example 1: Tracing a Linear Search (Foundation)
Question: The list [12, 7, 19, 4, 9] is searched for the value 4 using a linear search. Complete a trace table and state how many comparisons are made.
Solution:
We compare each item with the target 4, stopping when found.
| index | list[index] | list[index] == 4 ? | found |
|---|
| 0 | 12 | No | false |
| 1 | 7 | No | false |
| 2 | 19 | No | false |
| 3 | 4 | Yes | true |
Answer: The value is found at index 3 after 4 comparisons.
Exam Tip: State the index and the number of comparisons. The comparison count is what links to the efficiency discussion.
Example 2: Why Binary Search Needs an Ordered List (Stretch)
Question: A student tries to binary search the list [8, 3, 11, 1, 6] for the value 3. Explain why the search fails, then describe what must happen first.
Solution:
Binary search looks at the middle item (11 at index 2) and, on a sorted list, decides which half to discard. Since 3 < 11, it searches indices 0–1. The next midpoint is index 0, containing 8; since 3 < 8, the remaining interval becomes empty even though 3 is at index 1. Here the list is not sorted, so "everything to the left is smaller, everything to the right is larger" is not true. The algorithm could discard the half that actually contains the target and report it as missing.
To use binary search, the list must first be sorted into order, e.g. [1, 3, 6, 8, 11]. Only then does halving the list reliably keep the target in the chosen half.
Exam Tip: The marking point is the precondition: binary search requires an ordered list.
Example 3: One Pass of a Bubble Sort (Foundation)
Question: Show the result of the first pass of a bubble sort on [5, 2, 8, 1].
Solution:
We compare adjacent pairs and swap if the left item is larger.
| Step | Pair compared | Swap? | List after step |
|---|
| 1 | 5 and 2 | Yes | [2, 5, 8, 1] |
| 2 | 5 and 8 | No | [2, 5, 8, 1] |
| 3 | 8 and 1 | Yes | [2, 5, 1, 8] |
Answer after first pass: [2, 5, 1, 8]. The largest value 8 has "bubbled" to the end and is now in its final position.
Exam Tip: Use the words adjacent, compare and swap. Note that one full pass guarantees only the largest remaining value is placed.
Example 4: Tracing a Loop with Selection (Foundation/Higher)
Question: Trace this algorithm and state the output.
Python 3:
total = 0
for i in range(1, 6):
if i % 2 == 0:
total = total + i
print(total)
Solution:
i % 2 == 0 is true when i is even, so we add only even numbers.
| i | i % 2 == 0 ? | total |
|---|
| 1 | No | 0 |
| 2 | Yes | 2 |
| 3 | No | 2 |
| 4 | Yes | 6 |
| 5 | No | 6 |
Output: 6 (which is 2 + 4).
Exam Tip: Trace every iteration even when the value does not change — markers check that you tested the false cases too.
Example 5: Finding the Highest Score (Higher)
Question: A student enters five test scores. Write an algorithm that outputs the highest score, then trace it for the scores 18, 22, 15, 31, 29.
Solution — pseudocode:
Python 3 implementation:
highest = int(input("Score 1: "))
for count in range(2, 6):
score = int(input("Next score: "))
if score > highest:
highest = score
print(highest)
Assume five valid integer scores. Initialising from the first score also works when every score is negative.
Trace:
| count | score input | score > highest ? | highest |
|---|
| (start) | 18 | — | 18 |
| 2 | 22 | Yes | 22 |
| 3 | 15 | No | 22 |
| 4 | 31 | Yes | 31 |
| 5 | 29 | No | 31 |
Output: 31.
Common Mistake: Do not reset highest inside the loop — that destroys the best value found so far. Also note highest must be initialised to the first score before the loop, so there is a value to compare against.
Exam Tip: Initialising a "best so far" variable before the loop is a classic mark. State why: there must be a starting comparison value.
Example 6: Finding and Fixing a Logic Error (Higher/Stretch)
Question: This algorithm should count how many numbers in a list are greater than 10, but it always outputs 0. Identify the error and correct it.
Python 3 — deliberately faulty code:
values = [5, 14, 20]
count = 0
for value in values:
if value > 10:
count = count
print(count)
Solution:
The error is in the line count = count — it never actually increases the counter, so count stays at 0 regardless of the data. It should add one each time the condition is true.
Corrected line:
count = count + 1
So the working if block becomes:
if value > 10:
count = count + 1
How to catch this in the exam: Use a trace table with sample data such as [5, 14, 20]. Tracing shows count never changes when it should reach 2, pinpointing the faulty line.
Exam Tip: When asked to debug, always (a) name the exact line, (b) say what it does wrong, and (c) give the corrected line.