Revision Library

Data Structures and Their Operations - Worksheets, Questions and Revision

12 original exam-style questions - 5 pages of questions with a full mark scheme - free printable PDF.

Download PDFJump to mark scheme (page 6)Read the revision guide
« Previous: Programming Paradigms and OOPNext: Advanced Algorithms: Searching, Sorting and Path-Finding »
Revision Library
revisionlibrary.co.uk
A-Level · Component 02: Algorithms and Programming

A2.5 Data Structures and Their Operations

OCR H446 · Calculators not allowed · about 135 minutes
Total Marks
Name: _______________________________    Date: ____ / ____ / ______
Answer ALL questions. Show all your working.
1
A ticket-checking app used at a small theatre in Leeds implements a stack, a data structure that operates on a Last In First Out (LIFO) basis, to keep track of tickets as they are scanned.
(a)State what is meant by the term 'Last In First Out (LIFO)'.(1)
(b)Give one real-world example of the use of a stack, other than the scenario described above.(1)
(c)An empty stack is implemented using an array of maximum size 3 (indices 0 to 2), with a pointer called top that stores the index of the top item. When the stack is empty, top = -1. The following operations are performed, in order:
push(5), push(9), push(2), pop(), push(7)

Complete a trace table showing the contents of the array and the value of top after each of the five operations.
(4)
(d)Explain what would happen if a further operation, push(3), were attempted immediately after the operations in part (c).(2)
(Total for Question 1 is 8 marks)
2
A print-queue manager used in a school office in Norwich implements a linear queue, a data structure that operates on a First In First Out (FIFO) basis, using an array of size 5 (indices 0 to 4). Two pointers are used: front (the index of the item at the front of the queue) and rear (the index of the item most recently added). Initially front = 0 and rear = -1 (the queue is empty).
(a)State what is meant by the term 'First In First Out (FIFO)'.(1)
(b)The following operations are performed, in order, on the initially empty queue:
enqueue(A), enqueue(B), enqueue(C), dequeue(), enqueue(D)

Complete a trace table showing the array contents, front and rear after each operation.
(5)
(c)Explain one disadvantage of a linear queue, compared with a circular queue, in the context of the array above once index 0 has been vacated by the dequeue in part (b).(2)
(d)Describe how a circular queue would allow index 0 to be reused once rear reaches the last index of the array.(2)
(Total for Question 2 is 10 marks)
3
A student-run society at a university in Bristol keeps its members' ID numbers in a singly linked list, in the order they joined. Each node stores a data field and a pointer field holding the address of the next node; a pointer called head stores the address of the first node, and the last node's pointer field holds a null value.
(a)State the purpose of the pointer field in a node of a singly linked list.(1)
(b)The pseudocode below is intended to traverse a linked list from head and output every data value. Complete the two missing lines, (i) and (ii).
current <- head
WHILE ...........(i)........... DO
  OUTPUT current.data
  current <- ...........(ii)...........
ENDWHILE
(2)
(c)The list currently holds the ID numbers 10 -> 25 -> 40 (head points to the node holding 10; each arrow represents a pointer to the next node; the node holding 40 has a null pointer). A new node called newNode has already been created holding the value 18. Write pseudocode to insert newNode between the nodes holding 10 and 25, given that a pointer variable current already references the node holding 10.(3)
(d)State the value of head.next.next.data after the insertion in part (c).(1)
(Total for Question 3 is 7 marks)
4
The society's linked list from Question 3 now holds 10 -> 18 -> 25 -> 40, with head pointing to the node holding 10 and the node holding 40 having a null pointer.
(a)A pointer variable previous already references the node holding 18. Write pseudocode to delete the node holding 25 from the list.(3)
(b)A member of the society claims: 'Deleting a node from a linked list is always more efficient than deleting an item from the middle of an array.' Explain whether this claim is correct, referring to the number of elements that must be updated or moved in each structure.(3)
(c)State the Big O time complexity of searching for a given value in an unsorted singly linked list of n nodes, and justify your answer.(2)
(Total for Question 4 is 8 marks)
5
A calculator app developed by a startup in Manchester evaluates arithmetic expressions that have already been converted into postfix (Reverse Polish) notation, using a stack. Tokens are scanned left to right: when a number is read, it is pushed onto the stack; when an operator is read, the top two values are popped (the second-popped value, a, is the left operand and the first-popped value, b, is the right operand), the operation a operator b is applied, and the result is pushed back onto the stack.

Consider the postfix expression: 6 2 3 + * 4 -
(a)Complete a trace table showing the contents of the stack after each token in the expression has been processed.(6)
(b)State the final value of the expression.(1)
(c)Explain why a stack, rather than a queue, is the appropriate data structure for evaluating postfix expressions in this way.(2)
(Total for Question 5 is 9 marks)
6
A customer-service call centre in Cardiff models its call-holding system as a circular queue, implemented using an array of size 5 (indices 0 to 4), pointers front and rear, and a count of the number of calls currently held. Initially front = 0, rear = 0, count = 0. To enqueue: if count = 5 the queue is full; otherwise array[rear] <- item, rear <- (rear + 1) MOD 5, count <- count + 1. To dequeue: if count = 0 the queue is empty; otherwise item <- array[front], front <- (front + 1) MOD 5, count <- count - 1.

The following calls are held, in order: enqueue(P), enqueue(Q), enqueue(R), dequeue(), dequeue(), enqueue(S), enqueue(T), enqueue(U)
(a)Complete a trace table recording front, rear and count after each of the eight operations.(8)
(b)State the front-to-rear order of the calls remaining in the queue after all eight operations.(1)
(c)Explain why the array slot at index 0 can safely be reused for enqueue(U), even though it previously held the value P.(2)
(Total for Question 6 is 11 marks)
7
A library index system used in a school in Preston stores book reference numbers in a binary search tree (BST), so that they can be searched efficiently. Starting from an empty tree, the following values are inserted, one at a time, always starting comparisons at the root: 50, 30, 70, 20, 40, 60, 80, 35. The standard BST insertion rule is used: at each node, if the new value is less than the node's value, move left; otherwise, move right; the value is inserted at the first empty position reached.
(a)Determine the position of 35 in the completed tree, by comparing it against the root and each node subsequently reached, stating at each step whether the comparison sends it left or right. Hence state the parent of 35 and whether it becomes the left or right child of that parent.(3)
(b)State the value stored at the root of the completed tree.(1)
(c)Write out the pre-order traversal of the completed tree.(3)
(d)Write out the in-order traversal of the completed tree, and state what property of a binary search tree this result demonstrates.(3)
(Total for Question 7 is 10 marks)
8
A file-directory viewer developed by a software company in Oxford represents a small folder structure as a binary tree, where each node stores a single-letter identifier. The root node is M. M's left child is H and right child is T. H's left child is D and right child is L. T's left child is S and right child is W. Nodes D, L, S and W each have no children.
(a)Write out the result of a pre-order traversal of the tree.(2)
(b)Write out the result of an in-order traversal of the tree.(2)
(c)Write out the result of a post-order traversal of the tree.(2)
(d)Write out the result of a breadth-first (level-order) traversal of the tree, and state the abstract data type that is used to implement this traversal, giving a reason for its use.(3)
(Total for Question 8 is 9 marks)
9
A social-network analysis tool built by a data-science team in Sheffield models the friendship connections between five users, Aisha, Ben, Chidi, Priya and Tom, as an undirected graph, where an edge represents a mutual friendship. The friendships are: Aisha-Ben, Aisha-Chidi, Ben-Chidi, Ben-Priya, Chidi-Priya, Priya-Tom.
(a)Complete the adjacency matrix for this graph, using 1 to indicate a friendship and 0 to indicate no friendship, with rows and columns ordered alphabetically: Aisha, Ben, Chidi, Priya, Tom.(4)
(b)Write the adjacency list representation of the same graph.(3)
(c)State one advantage of using an adjacency list, rather than an adjacency matrix, to represent a sparse graph such as this one (a graph with relatively few edges compared with the number of possible edges).(2)
(Total for Question 9 is 9 marks)
10
A route-planning tool used by a delivery company in Leicester models six delivery hubs, Amir, Beth, Carlos, Dana, Elif and Farah, and the direct roads connecting them, as an undirected graph. The roads are: Amir-Beth, Amir-Carlos, Beth-Dana, Carlos-Dana, Carlos-Elif, Dana-Farah, Elif-Farah. When visiting the neighbours of a hub, they are always considered in alphabetical order.
(a)Perform a breadth-first search (BFS) of the graph starting at Amir, using a queue. Show the changing contents of the queue and state the order in which the hubs are visited.(4)
(b)Perform a depth-first search (DFS) of the graph starting at Amir, using a stack (or recursion), always exploring the first unvisited neighbour in alphabetical order before backtracking. State the order in which the hubs are visited.(4)
(c)Explain, with reference to the traversals performed in (a) and (b), one situation in which BFS would be preferred over DFS.(2)
(Total for Question 10 is 10 marks)
11
An online ticket-booking system used by a leisure centre in Swansea stores booking reference numbers in a hash table with 7 slots (indices 0 to 6), using the hash function h(k) = k MOD 7. Collisions are resolved using linear probing: if the calculated slot is already occupied, the next slot is tried (wrapping from slot 6 back to slot 0), and so on, until an empty slot is found.

The following keys are inserted, in this order, into an initially empty hash table: 23, 12, 15, 8, 19, 30
(a)Calculate h(k) for each of the six keys.(3)
(b)Complete a trace table showing the slot at which each key is finally stored, including any probing required due to collisions.(6)
(c)State the final contents of the hash table (slot index: key) for all 7 slots.(1)
(d)Calculate the load factor of the hash table after all six keys have been inserted, giving your answer as a fraction in its simplest form.(2)
(e)Explain one disadvantage of linear probing as a collision-resolution technique, compared with separate chaining.(2)
(Total for Question 11 is 14 marks)
12
A software engineer at a customer-records company based in Glasgow is designing a system that must store a large, frequently changing collection of customer records and needs to support fast insertion, deletion and look-up of a record by its unique customer ID.
(a)Discuss the suitability of a hash table, a binary search tree and a linked list for this purpose, comparing their typical time complexities for insertion, deletion and look-up, and recommend which structure the engineer should choose, justifying your recommendation.(9)
(Total for Question 12 is 9 marks)
Mark scheme · A2.5 Data Structures and Their Operations

Question 1

Question 2

Question 3

Question 4

Question 5

Question 6

Question 7

Question 8

Question 9

Question 10

Question 11

Question 12

Mark your answers

This checks your answers in your browser, stores nothing on a server and needs no account.

Question 1

8 marks
Did your answer earn the marks?

Question 2

10 marks
Did your answer earn the marks?

Question 3

7 marks
Did your answer earn the marks?

Question 4

8 marks
Did your answer earn the marks?

Question 5

9 marks
Did your answer earn the marks?

Question 6

11 marks
Did your answer earn the marks?

Question 7

10 marks
Did your answer earn the marks?

Question 8

9 marks
Did your answer earn the marks?

Question 9

9 marks
Did your answer earn the marks?

Question 10

10 marks
Did your answer earn the marks?

Question 11

14 marks
Did your answer earn the marks?

Question 12

9 marks
Did your answer earn the marks?
Mark my answers