Question bank › A-level Computer Science › Paper 1 – Programming, data structures & algorithms

Data structures (stacks, queues, graphs, trees, hash tables, dictionaries, vectors)

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core4 marks

(a) Explain the difference between a static data structure and a dynamic data structure. [2]

(b) A stack is described as a LIFO data structure and a queue as a FIFO data structure. Explain what is meant by each of these terms. [2]

2Standard5 marks

A circular queue is implemented using an array Q with six elements, indexed 0 to 5, and the variables front, rear and size. Initially the queue is empty with front = 0, rear = -1 and size = 0.

When an item is added, rear is updated using rear ← (rear + 1) MOD 6; when an item is removed, front ← (front + 1) MOD 6.

The following operations are carried out in order:

Enqueue(A), Enqueue(B), Enqueue(C), Enqueue(D), Dequeue, Dequeue, Enqueue(E), Enqueue(F), Enqueue(G)

(a) State the values of front, rear and size after these operations. [3]

(b) State the item stored at index 0 of the array. [1]

(c) Explain why a circular queue is a more efficient use of memory than a linear queue implemented in a fixed-size array. [1]

3Standard5 marks

A stack is implemented using an array and a top pointer. The array has indices 0 to 4. Initially the stack is empty and the top pointer is −1.

The following operations are carried out in order. Each value removed by POP is output.

PUSH(7), PUSH(3), POP, PUSH(9), PUSH(4), POP, POP, PUSH(2), PUSH(8)

(a) State the values that are output, in order. [2]

(b) State the contents of the stack, from bottom to top, and the value of the top pointer after all of the operations. [2]

(c) Describe the check that the PUSH operation should make before adding an item, and what should happen if the check fails. [1]

4Standard5 marks

The diagram shows a weighted, undirected graph.

(a) The graph is to be stored as an adjacency matrix. Complete rows C, D and E of the matrix, using 0 where there is no edge. [2]

ABCDE
A04200
B405100
C
D
E

(b) Explain why an adjacency list might be more suitable than an adjacency matrix for storing a graph with many vertices but few edges. [2]

(c) Explain why this graph is not a tree. [1]

4251034ABCDE
5Hard6 marks

A hash table has 11 locations, indexed 0 to 10. The hashing function is h(key) = key MOD 11. Collisions are handled by rehashing using linear probing (the next free location is used, wrapping round from 10 to 0).

Index012345678910
Key

(a) Show the contents of the hash table after the keys 23, 45, 70, 13, 81 are inserted in that order. [3]

(b) Explain the steps taken to search the table for the key 92 and state how many occupied locations are examined before the search ends. [2]

(c) State what is meant by a collision in a hash table. [1]

6Hard5 marks

A binary search tree is stored in an array of records. Each record has a Left pointer, a Data field and a Right pointer. A pointer value of −1 means there is no child. Items are inserted using alphabetical order: an item that comes before a node's data goes to the left of that node and an item that comes after it goes to the right. The items are inserted in the following order, with Mango as the root at index 0 and each later item stored at the next index:

Mango, Cherry, Peach, Apple, Fig, Plum, Banana, Kiwi

(a) Complete the Left and Right pointers in the table. [3]

IndexLeftDataRight
0Mango
1Cherry
2Peach
3Apple
4Fig
5Plum
6Banana
7Kiwi

(b) Describe how the tree would be searched to determine whether Grape is stored in it, stating the items compared. [2]

7Standard4 marks

The Python program below uses a dictionary.

words = "the cat sat on the mat the end".split()
counts = {}
for w in words:
    counts[w] = counts.get(w, 0) + 1
print(counts["the"], len(counts))

(a) State the output of the program. [2]

(b) Explain why a dictionary is a suitable data structure for counting how many times each word occurs in a very long document, rather than a list of [word, count] pairs. [2]

8Standard5 marks

The vectors u = [3, −1, 2] and v = [1, 4, −2] are defined over ℝ.

(a) Calculate u + v. [1]

(b) Calculate 2u − v. [1]

(c) Calculate the dot product u · v and state what the result tells you about the angle between the two vectors. [2]

(d) The vector w = 0.25u + 0.75v. Explain why w is a convex combination of u and v. [1]

Still losing marks on this topic?

Book a free introductory session and we'll find exactly which step is costing you.

Book a free intro