Computer Science · B4.1 · HL only

Abstract data types (HL)

Where data is organised by what you can do with it: linked lists, binary search trees, sets and hash tables. In 7Quest it is Data Vault: 4 stages, 47 quick questions and 12 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. Abstract data typesHL
    Explain what an abstract data type is and why programs describe data by its operations rather than its implementation.
    Syllabus B4.1.1, B4.1.6
  2. Linked listsHL
    Evaluate singly, doubly and circular linked lists, and construct their insertion, deletion, traversal and search.
    Syllabus B4.1.2, B4.1.3
  3. Binary search treesHL
    Explain how BSTs organise data and insert, delete, search and traverse their nodes.
    Syllabus B4.1.4
  4. Sets and hash tablesHL
    Construct and apply sets, and explain how hash tables store data with hashing, collision resolution and load factors.
    Syllabus B4.1.5, B4.1.6

Two questions to try

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

Explain4 marksHLSets and hash tables

Explain how a hash table that uses linear probing stores a new key and later finds it.

Show the markscheme

Markscheme (4 marks)

  1. A hash function calculates a bucket index from the key (e.g. key mod table size).
  2. If that bucket is empty the key is stored there; if it is occupied (a collision), the next bucket is tried, (h + 1) mod n, wrapping round to 0, until an empty bucket is found.
  3. To find the key, the same hash index is calculated and the buckets are checked in the same order from there.
  4. The search stops when the key is found, or when an empty bucket is reached / the whole table has been checked, meaning the key is not present.
  5. As the load factor rises, clusters of full buckets form and the probe sequences become longer / slower.
  6. (Award [1] per point, max 4.)

A full-marks answer

The hash function turns the key into a bucket index, for example key mod n for a table of n buckets. If that bucket is empty, the key is stored there. If it is already occupied, a collision has happened, so the table tries the next bucket, (h + 1) mod n, and keeps moving forward, wrapping round from the last bucket to bucket 0, until it finds an empty one, where the key is stored. To find the key later, the same hash index is calculated and the same sequence of buckets is checked. The search succeeds when it reaches the key and fails when it reaches an empty bucket, because the key would have been stored there. As the table fills up, clusters of occupied buckets make these sequences longer, which is why the load factor is kept low.

Construct10 marksHLChallengeLinked lists

A taxi company gives rides to its drivers in turn. The drivers on shift are kept in a circular singly linked list of Driver nodes, and self.current refers to the driver who will get the next ride.

(a) State what the next pointer of a driver holds when that driver is the only one on shift. [1]

(b) Construct the method count(self), which returns the number of drivers on shift. [4]

(c) Explain why count must not use the loop condition while node is not None. [2]

(d) A driver ends their shift. Without writing code, describe how their node is removed from the rota. [3]

class Driver:
    def __init__(self, name):
        self.name = name
        self.next = None

class Rota:
    def __init__(self):
        self.current = None      # None when nobody is on shift
Show the markscheme

Markscheme (10 marks)

  1. (a) A reference to itself / the same node (not None).
  2. (b) Returns 0 when self.current is None (empty rota).
  3. (b) Uses a temporary pointer that starts at self.current (or its next).
  4. (b) Loops following next until the pointer is back at the starting node (is self.current).
  5. (b) Increments a counter once for every node, including the first, and returns it.
  6. (c) In a circular list no next pointer is None: the last node points back to the first.
  7. (c) So the condition is always true and the loop never ends (infinite loop); it must stop on returning to the start.
  8. (d) Traverse from current to find the node whose next is the leaving driver (its predecessor), stopping if the search gets back to the start without finding them.
  9. (d) Set the predecessor’s next to the leaving driver’s next, so the circle closes around the gap.
  10. (d) If the leaving driver was current, move current on to the following driver; if they were the only driver, set current to None.

A full-marks answer

(a) It holds a reference to that same node: the driver points to themself, because the last node of a circular list always points back to the first.

(b)

def count(self):
    if self.current is None:
        return 0
    total = 1
    node = self.current.next
    while node is not self.current:
        total = total + 1
        node = node.next
    return total

(c) In a circular list no node’s next is None, since the last driver points back to the first. The condition node is not None would therefore always be true and the loop would never end. The loop has to stop when it gets back to the node it started from.

(d) Starting at current, follow the next pointers to find the node whose next is the leaving driver, which is their predecessor; if the search returns to the start without finding them, they are not on shift. Then set the predecessor’s next to the leaving driver’s next, so the circle is closed around the gap and the node is no longer reachable. If the leaving driver was current, current moves on to the next driver, so that the rota carries on in turn; if they were the only driver, current becomes None.

A taxi company gives rides to its drivers in turn. The drivers on shift are kept in a circular singly linked list of Driver nodes, and the field current of the class Rota refers to the driver who will get the next ride.

(a) State what the next pointer of a driver holds when that driver is the only one on shift. [1]

(b) Construct the method count() of Rota, which returns the number of drivers on shift. [4]

(c) Explain why count must not use the loop condition while (node != null). [2]

(d) A driver ends their shift. Without writing code, describe how their node is removed from the rota. [3]

class Driver {
    String name;
    Driver next;

    Driver(String name) {
        this.name = name;
        this.next = null;
    }
}

class Rota {
    private Driver current = null;    // null when nobody is on shift
}
Show the markscheme

Markscheme (10 marks)

  1. (a) A reference to itself / the same node (not null).
  2. (b) Returns 0 when current is null (empty rota).
  3. (b) Uses a temporary pointer that starts at current (or current.next).
  4. (b) Loops following next until the pointer is back at the starting node (node != current, comparing references).
  5. (b) Increments a counter once for every node, including the first, and returns it.
  6. (c) In a circular list no next pointer is null: the last node points back to the first.
  7. (c) So the condition is always true and the loop never ends (infinite loop); it must stop on returning to the start.
  8. (d) Traverse from current to find the node whose next is the leaving driver (its predecessor), stopping if the search gets back to the start without finding them.
  9. (d) Set the predecessor’s next to the leaving driver’s next, so the circle closes around the gap.
  10. (d) If the leaving driver was current, move current on to the following driver; if they were the only driver, set current to null.

A full-marks answer

(a) It holds a reference to that same node: the driver points to themself, because the last node of a circular list always points back to the first.

(b)

public int count() {
    if (current == null) {
        return 0;
    }
    int total = 1;
    Driver node = current.next;
    while (node != current) {
        total = total + 1;
        node = node.next;
    }
    return total;
}

(c) In a circular list no node’s next is null, since the last driver points back to the first. The condition node != null would therefore always be true and the loop would never end. The loop has to stop when it gets back to the node it started from.

(d) Starting at current, follow the next pointers to find the node whose next is the leaving driver, which is their predecessor; if the search returns to the start without finding them, they are not on shift. Then set the predecessor’s next to the leaving driver’s next, so the circle is closed around the gap and the node is no longer reachable. If the leaving driver was current, current moves on to the next driver, so that the rota carries on in turn; if they were the only driver, current becomes null.

The other 10 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