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).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

What an algorithm is

An algorithm is a finite sequence of unambiguous steps that solves a problem or completes a task. Good algorithms are:

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:

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)
11
310
535
784

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) ```

  1. Every value is negative, so n > 0 is never true.
  2. biggest stays 0, which is not in the list.
  3. Fix: start with the first item, biggest = nums[0].

Answer: Initialise biggest = nums[0]; output is then -2.

Key idea: Algorithm: finite, unambiguous steps. Flowchart: terminator, process, input/output, decision. Efficiency: how steps grow with n. Logic errors give wrong results; trace tables find them.

Check yourself

  1. 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.

  2. 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.

  3. 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.

  4. Which flowchart symbol represents a decision?
    • Rectangle
    • Parallelogram
    • Rounded rectangle (terminator)
    • Diamond
    Show answer

    Diamond. A decision has a yes and a no exit.

  5. 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.

  6. 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).

  7. 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.

  8. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.