All subjects · Computer Science · WCP01 Principles of Computer Science
Algorithms, efficiency, logic errors and trace tables
Edexcel IAL Computer Science · WCP01 Principles of Computer Science
An algorithm is a precise sequence of steps that solves a problem. You need to read and complete algorithms in words, flowcharts and Python code, judge efficiency, fix logic errors and complete trace tables (spec points 5.1.1 and 5.1.2).
What an algorithm is
An algorithm is a finite sequence of unambiguous steps that solves a problem or completes a task. Good algorithms are:
- finite: they stop;
- unambiguous: each step has one meaning;
- clear about inputs and outputs;
- correct for all valid inputs, including edge cases.
Representations: written description, flowchart (terminator for start and end, rectangle for a process, parallelogram for input/output, diamond for a decision, arrows for flow), and code (Python in the Pearson papers). Algorithms use three constructs: sequence, selection (if) and iteration (loops).
Efficiency
Efficiency is about how the number of steps (time) or amount of memory grows as the input gets bigger. For a list of n items:
- linear search may need up to n comparisons;
- binary search needs at most about log2 n comparisons (for example 10 for 1000 items);
- bubble sort and insertion sort may need about n2/2 comparisons.
A more efficient algorithm matters most when n is large.
Logic errors and trace tables
A logic error makes a program run but give the wrong result, for example using < instead of <=, starting a counter at the wrong value, or updating a variable in the wrong place. (A syntax error breaks the rules of the language and stops it running.)
A trace table records the value of each variable (and any output) every time it changes, line by line. Use one row per change, and only fill a cell when that variable changes. Tracing reveals logic errors by showing where values go wrong.
Example trace
total = 0
i = 1
while i <= 7:
total = total + i * i
i = i + 2
print(total)| i (at the start of the pass) | total (after the pass) |
|---|---|
| 1 | 1 |
| 3 | 10 |
| 5 | 35 |
| 7 | 84 |
The loop stops when i reaches 9. Output: 84
Worked example
This code should output the largest value in nums but outputs the wrong answer for [-4, -9, -2]. Find and fix the logic error. ```python biggest = 0 for n in nums: if n > biggest: biggest = n print(biggest) ```
- Every value is negative, so n > 0 is never true.
- biggest stays 0, which is not in the list.
- Fix: start with the first item, biggest = nums[0].
Answer: Initialise biggest = nums[0]; output is then -2.
Check yourself
- Trace: total = 0, i = 1; while i <= 7: total = total + i * i; i = i + 2. What is printed after the loop?
Show answer
84. i takes 1, 3, 5, 7: 1 + 9 + 25 + 49 = 84.
- In the loop i = 1; while i <= 7: (body); i = i + 2, how many times does the loop body run?
Show answer
4. i = 1, 3, 5, 7: 4 times.
- Trace: x = 2; for k in range(3): x = x * 3 - 1. What is x afterwards?
Show answer
41. 2 becomes 5, then 14, then 41.
- Which flowchart symbol represents a decision?
- Rectangle
- Parallelogram
- Rounded rectangle (terminator)
- Diamond
Show answer
Diamond. A decision has a yes and a no exit.
- A program to count the numbers 1 to 10 uses while n < 10, starting at n = 1, and counts only 9 numbers. What type of error is this?
- Syntax error
- Logic error
- Runtime error caused by dividing by zero
- Hardware error
Show answer
Logic error. It runs but gives the wrong result; the condition should be n <= 10.
- Approximately how many comparisons does binary search need, at most, to search a sorted list of 1024 items?
Show answer
11. Each comparison halves the list: 1024 halves 10 times to 1 item, so at most 11 comparisons (log2 1024 = 10, plus the final check).
- Which property must every algorithm have?
- It must use a loop
- It must stop after a finite number of steps
- It must be written in Python
- It must sort a list
Show answer
It must stop after a finite number of steps. An algorithm that never ends does not solve the problem.
- Trace: c = 0; for v in [4, 7, 10, 13, 16]: if v % 2 == 1: c = c + v. What is c?
Show answer
20. Only 7 and 13 are odd: 7 + 13 = 20.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (2026)
- Pearson Edexcel IAL Computer Science IAS sample assessment materials (Unit 1, WCP01)
Last updated 8 November 2026. Spotted a mistake? Tell us.