Computer Science · B2.2, B2.4–B2.5

Data structures, algorithms and files

Lists, stacks, queues, Big O, searching, sorting and files: the engine that drives Paper 2. In 7Quest it is Algorithm Engine: 7 stages, 59 quick questions and 17 exam-style ones.

What the stages cover

Codes follow the 2027 guide (A1–A4 and B1–B4). HL-only stages are marked. Each stage is a short lesson, quick questions, a minigame and an exam-style question.

  1. Lists and 2D lists
    Distinguish static and dynamic data structures, and traverse, modify and analyse 1D and 2D lists using nested loops.
    Syllabus B2.2.1, B2.2.2
  2. Stacks and queues
    Apply the operations of a stack (LIFO) and a queue (FIFO), implement them with lists and choose the right one for each problem.
    Syllabus B2.2.3, B2.2.4
  3. Big O notation
    Determine the time and space complexity of an algorithm from its code and reason about its scalability.
    Syllabus B2.4.1
  4. Linear and binary search
    Construct and trace linear and binary searches, know their preconditions and compare their efficiency.
    Syllabus B2.4.2
  5. Bubble sort and selection sort
    Construct, trace and compare bubble sort (including the optimised version) and selection sort, counting passes, comparisons and swaps.
    Syllabus B2.4.3
  6. RecursionHL
    Explain recursion, its base case and the call stack, compare it with iteration, and construct and trace recursive algorithms.
    Syllabus B2.4.4, B2.4.5
  7. File processing
    Open, read, write and append data to text files, process CSV-style lines and handle a file that does not exist.
    Syllabus B2.5.1

Two questions to try

Written for 7Quest in the style of the exam, not taken from past papers. Answer first, then open the markscheme.

Explain3 marksStacks and queues

A school has one printer shared by all its classrooms. Explain why a queue is a more appropriate data structure than a stack for holding the print jobs.

Show the markscheme

Markscheme (3 marks)

  1. A queue is FIFO: the first job sent is the first to be printed.
  2. Therefore jobs are printed in order of arrival / fairly.
  3. With a stack (LIFO) the last job sent would be printed first, so the earliest jobs could wait indefinitely (starvation) if new jobs keep arriving.

A full-marks answer

A queue works on a FIFO basis: jobs are added at the rear and the printer always takes the one at the front, that is, the one that has been waiting longest. In this way documents are printed in the same order in which they were sent, which is fair to all users. If a stack (LIFO) were used, the most recently received job would always be printed first; at a busy time, the earliest jobs would stay at the bottom and might never be printed while others kept arriving.

Construct10 marksHLChallengeRecursion

Consider the recursive function collapse.

(a) Identify the base case. [1]

(b) Trace the call collapse(587), showing each call that is made and the value it returns. [3]

(c) State the maximum number of stack frames for collapse that exist at the same time during collapse(587). [1]

(d) Construct an iterative version of collapse that returns the same result for any non-negative integer. [3]

(e) Explain why the iterative version uses less memory than the recursive version. [2]

def collapse(n):
    if n < 10:
        return n
    return collapse(n // 10 + n % 10)
Show the markscheme

Markscheme (10 marks)

  1. (a) n < 10: a single-digit number is returned directly, without a recursive call.
  2. (Award [1] for each correct stage of the trace in part b, max 3. If only the final value 2 is given, without the calls, award [1] max.)
  3. (b) collapse(587) calls collapse(65), because 58 + 7 = 65.
  4. (b) collapse(65) calls collapse(11) (6 + 5), which calls collapse(2) (1 + 1).
  5. (b) collapse(2) returns 2 (base case) and each waiting call returns that same 2, so the result is 2.
  6. (c) 4 frames (for 587, 65, 11 and 2).
  7. (d) A loop that repeats while n >= 10 (or n > 9).
  8. (d) Inside the loop, n is replaced by n // 10 + n % 10.
  9. (d) Returns n after the loop has finished.
  10. (e) The recursive version pushes a new stack frame (parameters, return address) for every call until the base case is reached…
  11. (e) …whereas the loop updates the same variable inside a single frame, so its memory use stays constant, O(1)O(1).

A full-marks answer

(a) The base case is n < 10: a one-digit number is returned as it is.

(b) collapse(587) → 58 + 7 = 65 → collapse(65) → 6 + 5 = 11 → collapse(11) → 1 + 1 = 2 → collapse(2), which is the base case and returns 2. Unwinding, collapse(11), collapse(65) and collapse(587) each return that 2, so the result is 2.

(c) 4 frames: none of the four calls finishes until collapse(2) returns.

(d)

def collapse_iter(n):
    while n >= 10:
        n = n // 10 + n % 10
    return n

(e) Each recursive call adds a stack frame that stays on the stack until the base case is reached, so the memory used grows with the number of calls. The loop simply overwrites n inside one frame, so it needs the same small amount of memory however many repetitions there are.

Consider the recursive method collapse.

(a) Identify the base case. [1]

(b) Trace the call collapse(587), showing each call that is made and the value it returns. [3]

(c) State the maximum number of stack frames for collapse that exist at the same time during collapse(587). [1]

(d) Construct an iterative version of collapse that returns the same result for any non-negative integer. [3]

(e) Explain why the iterative version uses less memory than the recursive version. [2]

public static int collapse(int n) {
    if (n < 10) {
        return n;
    }
    return collapse(n / 10 + n % 10);
}
Show the markscheme

Markscheme (10 marks)

  1. (a) n < 10: a single-digit number is returned directly, without a recursive call.
  2. (Award [1] for each correct stage of the trace in part b, max 3. If only the final value 2 is given, without the calls, award [1] max.)
  3. (b) collapse(587) calls collapse(65), because 58 + 7 = 65 (587 / 10 is 58 with int division).
  4. (b) collapse(65) calls collapse(11) (6 + 5), which calls collapse(2) (1 + 1).
  5. (b) collapse(2) returns 2 (base case) and each waiting call returns that same 2, so the result is 2.
  6. (c) 4 frames (for 587, 65, 11 and 2).
  7. (d) A loop that repeats while n >= 10 (or n > 9).
  8. (d) Inside the loop, n is replaced by n / 10 + n % 10.
  9. (d) Returns n after the loop has finished.
  10. (e) The recursive version pushes a new stack frame (parameters, return address) for every call until the base case is reached…
  11. (e) …whereas the loop updates the same variable inside a single frame, so its memory use stays constant, O(1)O(1).

A full-marks answer

(a) The base case is n < 10: a one-digit number is returned as it is.

(b) collapse(587) → 58 + 7 = 65 → collapse(65) → 6 + 5 = 11 → collapse(11) → 1 + 1 = 2 → collapse(2), which is the base case and returns 2. Unwinding, collapse(11), collapse(65) and collapse(587) each return that 2, so the result is 2.

(c) 4 frames: none of the four calls finishes until collapse(2) returns.

(d)

public static int collapseIter(int n) {
    while (n >= 10) {
        n = n / 10 + n % 10;
    }
    return n;
}

(e) Each recursive call adds a stack frame that stays on the stack until the base case is reached, so the memory used grows with the number of calls. The loop simply overwrites n inside one frame, so it needs the same small amount of memory however many repetitions there are.

The other 15 questions in this topic are in the app, marked point by point, and anything you miss comes back in your review. If this is the topic you find hardest, you can make it your free topic and do all of it, with short practice tests on it, without paying or giving a card.

Start Computer Science free