Question Simulator

AS & A Level Computer Science (9618)

Computational thinking and problem-solving Banker

9 questions · about 4% of the marks we indexed · examined in 9 of 9 sessions.

Questions by year

2026

  • 2026 May/June · Paper 32 · Q10 (a)3 marksSearching & Big O

2025

  • 2025 Oct/Nov · Paper 32 · Q12 (a(i), a(ii), b)9 marksAbstract data types · Recursion
  • 2025 May/June · Paper 32 · Q11 (a, b)6 marksAbstract data types · Recursion

2024

  • 2024 Oct/Nov · Paper 32 · Q11 (a, b, c)12 marksAbstract data types
  • 2024 May/June · Paper 32 · Q8 (a, b)8 marksSearching & Big O

2023

  • 2023 Oct/Nov · Paper 32 · Q9 (a(i), a(ii), b, c)13 marksAbstract data types · Recursion
  • 2023 May/June · Paper 32 · Q11 (a(i), a(ii), b)10 marksAbstract data types

2022

  • 2022 Oct/Nov · Paper 32 · Q11 (a, b, c)10 marksAbstract data types
  • 2022 May/June · Paper 32 · Q8 (a, b(i), b(ii), b(iii), c)13 marksSearching & Big O

References only, not question text. Open Official paper ↗ where Cambridge publishes the paper, or see the official Cambridge past-papers page; for other papers, ask your school. “Paper 42” means Paper 4, variant 2.

Practice questions

Original questions written by HomiLabs in the style of the exam, with answers and mark schemes. They are not Cambridge past paper questions and have not been reviewed by Cambridge. Try each one before opening the answer.

Q1Abstract data types · Core · Paper 3 style · 5 marks
  1. (a) A stack is stored in a 1D array Stack[1:6]. TopPointer holds the index of the top item and is 0 when the stack is empty. The stack starts empty. These operations are carried out in order:PUSH 14, PUSH 27, PUSH 8, POP, PUSH 31, PUSH 5, POP, POPState the values removed by the POP operations, in order. [2]
  2. (b) State the final value of TopPointer and the contents of Stack[1] to Stack[4] after all the operations. [3]
Show answer
  1. (a) 8, 5, 31
    2 marks all three in order; 1 mark for the correct three values in the wrong order.
  2. (b) TopPointer = 2. Stack[1] = 14, Stack[2] = 27, Stack[3] = 31, Stack[4] = 5 (positions 3 and 4 are no longer part of the stack, but popping does not erase their values).
    1 mark: TopPointer 2. 1 mark: Stack[1] and Stack[2] correct. 1 mark: Stack[3] = 31 and Stack[4] = 5 (accept them shown as empty/not in stack if the student explains they are no longer in use).
Q2Abstract data types · Core · Paper 3 style · 6 marks
  1. (a) A circular queue is stored in an array Queue[0:4]. FrontPointer starts at 0, RearPointer starts at −1 and NumItems at 0. When RearPointer or FrontPointer moves past 4 it wraps round to 0.These operations are carried out in order: Enqueue A, Enqueue B, Enqueue C, Dequeue, Enqueue D, Enqueue E, Dequeue, Dequeue, Enqueue F, Enqueue G.State the items removed by the Dequeue operations, and the final values of FrontPointer, RearPointer and NumItems. [4]
  2. (b) Enqueue H is now carried out. State the index where H is stored and the new value of NumItems, and explain what would happen if Enqueue J were then attempted. [2]
Show answer
  1. (a) Removed: A, then B, then C. FrontPointer = 3, RearPointer = 1, NumItems = 4.
    Trace: A at 0, B at 1, C at 2 (Rear 2); Dequeue A (Front 1); D at 3, E at 4 (Rear 4); Dequeue B (Front 2), Dequeue C (Front 3); F at 0, G at 1 (Rear wraps to 0 then 1). 1 mark: A, B, C in order. 1 mark: FrontPointer 3. 1 mark: RearPointer 1. 1 mark: NumItems 4.
  2. (b) H is stored at index 2 (RearPointer becomes 2) and NumItems becomes 5. Enqueue J would fail / 'queue full' would be reported, because NumItems now equals the array size (5).
    1 mark: index 2 and NumItems 5. 1 mark: J rejected because the queue is full (NumItems = 5).
Q3Abstract data types · Core · Paper 3 style · 6 marks
  1. (a) Describe the dictionary abstract data type. [2]
  2. (b) An app for learners of Urdu stores English words with their Urdu translations. Explain why a dictionary is a suitable ADT for this, rather than a 1D array. [2]
  3. (c) State two operations that a dictionary ADT provides. [2]
Show answer
  1. (a) A collection of key–value pairs: each value is stored with a unique key, and the value is found (looked up) using its key rather than an index position.
    1 mark: pairs of keys and values. 1 mark: access via unique key.
  2. (b) The user looks up a word directly by the English word (the key) to get the translation (the value); with an array, the program would need to search through the elements (or keep two parallel arrays) to find the word.
    1 mark: lookup by key/word. 1 mark: array would need searching/indexes don't relate to the word.
  3. (c) Any two of: add a key–value pair; look up/retrieve the value for a key; delete a pair by key; check whether a key exists; update the value for a key.
    1 mark each.
Q4Abstract data types · Core · Paper 3 style · 6 marks
  1. (a) Explain the difference between a stack and a queue. [2]
  2. (b) For each use, state whether a stack or a queue is more suitable.(i) Holding print jobs sent to a shared printer.(ii) Storing return addresses when procedures call other procedures.(iii) The 'undo' feature in a text editor.(iv) Buffering keystrokes typed faster than a program can process them. [4]
Show answer
  1. (a) A stack is last in, first out (LIFO): items are added and removed at the same end (the top). A queue is first in, first out (FIFO): items are added at the rear and removed from the front.
    1 mark each.
  2. (b) (i) Queue (ii) Stack (iii) Stack (iv) Queue
    1 mark each.
Q5Abstract data types · Core · Paper 3 style · 5 marks
  1. (a) Give two advantages of implementing a list as a linked list rather than as an ordered array. [2]
  2. (b) Give one disadvantage of a linked list compared with an array. [1]
  3. (c) A linked list implemented in arrays also has a free list. Explain the purpose of the free list. [2]
Show answer
  1. (a) Any two of: items can be inserted or deleted by changing pointers, without moving other items; (with dynamic memory) the list can grow and shrink as needed; insertion/deletion in the middle is quicker for large lists.
    1 mark each.
  2. (b) Items cannot be accessed directly by index – the list must be followed from the start (so binary search cannot be used); extra memory is needed for the pointers.
    B1.
  3. (c) It links together all the unused nodes, so that when an item is inserted the next available node can be found immediately (from FreePointer), and deleted nodes can be returned to it to be reused.
    1 mark: links unused nodes. 1 mark: provides a node on insertion / reuses deleted nodes.
Q6Abstract data types · Core · Paper 3 style · 5 marks
  1. (a) A linked list of student names uses nodes created dynamically. Write pseudocode to declare:(i) a pointer type TNodePointer that points to a TNode(ii) a record type TNode with a field Name (STRING) and a field Next (TNodePointer)(iii) a variable StartPointer of type TNodePointer. [4]
  2. (b) Explain why TYPE is used in (i) and (ii) but DECLARE in (iii). [1]
Show answer
  1. (a) (i) TYPE TNodePointer = ^TNode(ii) TYPE TNode DECLARE Name : STRING DECLARE Next : TNodePointerENDTYPE(iii) DECLARE StartPointer : TNodePointer
    1 mark for (i). 2 marks for (ii): TYPE … ENDTYPE structure; both fields with correct types. 1 mark for (iii).
  2. (b) TYPE defines a new data type (a template, no storage is created); DECLARE creates a variable of a type.
    B1.
Q7Abstract data types · Standard · Paper 3 style · 8 marks

A linked list of fruit names is held in two arrays, Data[0:7] and Pointer[0:7]:Index | Data | Pointer0 | "Mango" | 41 | "Apple" | 32 | (empty) | 63 | "Banana" | 04 | "Peach" | −15 | (empty) | 26 | (empty) | 77 | (empty) | −1StartPointer = 1, FreePointer = 5. The null pointer is −1.

  1. (a) State the items in the linked list in the order they are visited from StartPointer. [1]
  2. (b) "Cherry" is inserted into the list so that it stays in alphabetical order. State the new values of StartPointer and FreePointer, and every Data or Pointer value that changes. [4]
  3. (c) "Apple" is then deleted, and its node is added to the start of the free list. State the new values of StartPointer, FreePointer and Pointer[1]. [3]
Show answer
  1. (a) Apple, Banana, Mango, Peach
    B1.
  2. (b) Data[5] = "Cherry"; Pointer[5] = 0; Pointer[3] = 5; FreePointer = 2; StartPointer stays 1.
    1 mark: Cherry stored in node 5. 1 mark: Pointer[5] = 0 (points to Mango). 1 mark: Pointer[3] = 5 (Banana points to Cherry). 1 mark: FreePointer = 2 and StartPointer 1.
  3. (c) StartPointer = 3; FreePointer = 1; Pointer[1] = 2.
    1 mark each.
Q8Abstract data types · Standard · Paper 3 style · 7 marks
  1. (a) The names Kiran, Daud, Rafay, Bano, Haris, Saima, Ehsan are inserted in this order into an empty binary search tree (alphabetical order, smaller to the left). The tree is stored in arrays Data[0:6], LeftPointer[0:6] and RightPointer[0:6], each name going into the next free index starting at 0. A null pointer is −1. Give the LeftPointer and RightPointer values for every index. [4]
  2. (b) Write the order in which the nodes are visited in (i) an in-order traversal (ii) a post-order traversal. [3]
Show answer
  1. (a) Index | Data | Left | Right0 | Kiran | 1 | 21 | Daud | 3 | 42 | Rafay | −1 | 53 | Bano | −1 | −14 | Haris | 6 | −15 | Saima | −1 | −16 | Ehsan | −1 | −1
    1 mark each for: row 0 correct; row 1 correct; rows 2 and 4 correct; all leaf rows (3, 5, 6) −1, −1.
  2. (b) (i) Bano, Daud, Ehsan, Haris, Kiran, Rafay, Saima (ii) Bano, Ehsan, Haris, Daud, Saima, Rafay, Kiran
    1 mark for (i). 2 marks for (ii); 1 mark if one pair is out of order.
Q9Abstract data types · Standard · Paper 3 style · 6 marks
  1. (a) A delivery company models five depots P, Q, R, S and T as an undirected graph. Roads join P–Q, P–R, Q–S, R–S and S–T. Write the adjacency matrix for the graph, using 1 for a road and 0 for no road, with rows and columns in the order P, Q, R, S, T. [3]
  2. (b) State the number of 1s in the matrix and explain why the matrix is symmetrical. [2]
  3. (c) Describe how the matrix would change if each road were given a distance in kilometres. [1]
Show answer
  1. (a) P Q R S TP 0 1 1 0 0Q 1 0 0 1 0R 1 0 0 1 0S 0 1 1 0 1T 0 0 0 1 0
    3 marks all correct and symmetric; 2 marks for one error (or its mirror); 1 mark for two errors.
  2. (b) 10 ones. The graph is undirected: a road from X to Y is also a road from Y to X, so each edge appears twice (row X column Y and row Y column X).
    1 mark: 10. 1 mark: explanation.
  3. (c) Each 1 would be replaced by the distance (weight) of that road; 0 (or ∞/blank) still shows no road.
    B1.
Q10Abstract data types · Standard · Paper 3 style · 7 marks
  1. (a) A circular queue of patient numbers at a clinic is stored in the global array Queue[0:19] with global variables FrontPointer, RearPointer and NumItems (initially 0, −1 and 0).Write a pseudocode procedure Enqueue(Patient : INTEGER) that adds Patient to the rear of the queue, or outputs "Queue full" if there is no space. [5]
  2. (b) Explain why a circular queue makes better use of the array than a linear queue. [2]
Show answer
  1. (a) PROCEDURE Enqueue(Patient : INTEGER) IF NumItems = 20 THEN OUTPUT "Queue full" ELSE RearPointer ← RearPointer + 1 IF RearPointer > 19 THEN RearPointer ← 0 ENDIF Queue[RearPointer] ← Patient NumItems ← NumItems + 1 ENDIFENDPROCEDURE
    1 mark each for: check NumItems = 20 with message; increment RearPointer; wrap RearPointer to 0 after 19 (or use MOD 20); store Patient at Queue[RearPointer]; increment NumItems.
  2. (b) In a linear queue, spaces at the front freed by dequeuing cannot be reused, so the queue may report full when there is space; in a circular queue the pointers wrap round so freed spaces at the start are reused.
    1 mark: linear queue wastes freed spaces at front. 1 mark: circular queue wraps round and reuses them.
Q11Abstract data types · Standard · Paper 4 style · 11 marks
  1. (a) Write program code for a class Stack, implemented with an array/list of fixed size, with methods Push(Item) (returns False if full), Pop() (returns None/null if empty) and IsEmpty(). The top pointer must be a private attribute.Then write a function Balanced(Expression) that uses a Stack to return True if every bracket ( [ { in Expression is closed by the matching bracket in the correct order, and False otherwise. (Python shown; Java or VB.NET solutions are equally acceptable.) [9]
  2. (b) State the value returned by Balanced for each test string: "(a+b)*[c-d]", "{x*(y+z]}", "((p+q)", "[(m/n)+{r*s}]". [2]
Show answer
  1. (a) class Stack: def __init__(self, size): self.__items = [""] * size self.__top = -1 # -1 means empty self.__size = size def push(self, item): if self.__top == self.__size - 1: return False # stack full self.__top = self.__top + 1 self.__items[self.__top] = item return True def pop(self): if self.__top == -1: return None # stack empty item = self.__items[self.__top] self.__top = self.__top - 1 return item def is_empty(self): return self.__top == -1def balanced(expression): pairs = {")": "(", "]": "[", "}": "{"} s = Stack(len(expression)) for ch in expression: if ch in "([{": s.push(ch) elif ch in ")]}": if s.pop() != pairs[ch]: # empty stack or wrong type of bracket return False return s.is_empty()
    1 mark each for: class with private array, top pointer and size; Push checks full, increments top then stores; Pop checks empty, reads then decrements; IsEmpty; Balanced creates a stack; loops through each character; pushes opening brackets; for a closing bracket pops and compares with the matching opening bracket, returning False on mismatch or empty stack; returns True only if the stack is empty at the end.
  2. (b) True, False, False, True
    2 marks all four correct; 1 mark for three.
Q12Abstract data types · Standard · Paper 4 style · 11 marks
  1. (a) A bank branch in Hyderabad issues numbered tokens to customers. Waiting tokens are stored in a circular queue of size 5 using a global array Queue, and global variables Front (0), Rear (−1) and NumItems (0).Write program code for:• a function Enqueue(Token) that adds a token and returns True, or returns False if the queue is full• a function Dequeue() that removes and returns the token at the front, or returns −1 if the queue is empty. (Python shown; Java or VB.NET solutions are equally acceptable.) [7]
  2. (b) Tokens 101, 102, 103, 104 are enqueued, then Dequeue is called twice, then tokens 105, 106, 107 and 108 are each enqueued. State the values returned by the two Dequeue calls, which of the last four Enqueue calls return False, and the final values of Front and Rear. [4]
Show answer
  1. (a) SIZE = 5queue = [0] * SIZEfront = 0rear = -1num_items = 0def enqueue(token): global rear, num_items if num_items == SIZE: return False # queue full rear = (rear + 1) % SIZE # wrap round to 0 after the last index queue[rear] = token num_items = num_items + 1 return Truedef dequeue(): global front, num_items if num_items == 0: return -1 # queue empty token = queue[front] front = (front + 1) % SIZE num_items = num_items - 1 return token
    1 mark each for: globals initialised; Enqueue checks full (NumItems = size); Rear moved on with wrap-round (MOD or IF); token stored and NumItems incremented, returning True; Dequeue checks empty and returns −1; reads token at Front; moves Front with wrap-round and decrements NumItems.
  2. (b) Dequeue returns 101 then 102. Only Enqueue(108) returns False (the queue is full with 103–107). Front = 2, Rear = 1.
    1 mark: 101, 102. 1 mark: only 108 fails. 1 mark: Front 2. 1 mark: Rear 1 (105 at 4, 106 at 0, 107 at 1).
Q13Abstract data types · Challenge · Paper 3 style · 7 marks
  1. (a) A linked list is stored in the array List[0:9] of records of type TNode:TYPE TNode DECLARE Data : STRING DECLARE Pointer : INTEGERENDTYPEGlobal variables StartPointer and FreeListPointer hold the indices of the first node and the first free node; NullPointer is a constant −1.Write a pseudocode function DeleteNode(Item : STRING) RETURNS BOOLEAN that removes the first node whose Data is Item, adds that node to the front of the free list, and returns TRUE. It returns FALSE if Item is not in the list. [7]
Show answer
  1. (a) FUNCTION DeleteNode(Item : STRING) RETURNS BOOLEAN DECLARE Current, Previous : INTEGER Current ← StartPointer Previous ← NullPointer WHILE Current <> NullPointer AND List[Current].Data <> Item Previous ← Current Current ← List[Current].Pointer ENDWHILE IF Current = NullPointer THEN RETURN FALSE ENDIF IF Previous = NullPointer THEN StartPointer ← List[Current].Pointer ELSE List[Previous].Pointer ← List[Current].Pointer ENDIF List[Current].Pointer ← FreeListPointer FreeListPointer ← Current RETURN TRUEENDFUNCTION
    1 mark each for: start at StartPointer with Previous set to null; loop while not at the end and item not found; move Previous and Current along the list; return FALSE if the end is reached; if the node is first, update StartPointer; otherwise link Previous to the node after Current; link the deleted node into the free list (Pointer ← FreeListPointer, FreeListPointer ← Current) and return TRUE.
Q14Abstract data types · Challenge · Paper 3 style · 8 marks
  1. (a) A stack of up to 10 integers is stored in the global array Stack[1:10]. The global variable TopPointer is 0 when the stack is empty.Write pseudocode for:• a function Push(Value : INTEGER) RETURNS BOOLEAN that adds Value to the stack and returns TRUE, or returns FALSE if the stack is full• a function Pop() RETURNS INTEGER that removes and returns the top value, or returns −1 if the stack is empty. [7]
  2. (b) Explain why the order of the two statements that change TopPointer and access the array is different in Push and Pop. [1]
Show answer
  1. (a) FUNCTION Push(Value : INTEGER) RETURNS BOOLEAN IF TopPointer = 10 THEN RETURN FALSE ENDIF TopPointer ← TopPointer + 1 Stack[TopPointer] ← Value RETURN TRUEENDFUNCTIONFUNCTION Pop() RETURNS INTEGER DECLARE Value : INTEGER IF TopPointer = 0 THEN RETURN -1 ENDIF Value ← Stack[TopPointer] TopPointer ← TopPointer - 1 RETURN ValueENDFUNCTION
    Push (4): 1 mark check for full (TopPointer = 10); 1 mark return FALSE when full; 1 mark increment TopPointer before storing; 1 mark store Value and return TRUE. Pop (3): 1 mark check empty and return −1; 1 mark take value from Stack[TopPointer]; 1 mark decrement TopPointer after reading and return the value.
  2. (b) TopPointer points to the current top item: Push must first move to the next free space before storing, while Pop must read the top item before moving the pointer down.
    B1.
Q15Abstract data types · Challenge · Paper 4 style · 13 marks
  1. (a) A program keeps town names in alphabetical order using a linked list stored in two arrays of 8 elements, Data (strings) and Pointer (integers). StartPointer is initially −1 (null) and FreePointer is 0; at the start every node is in the free list, with Pointer[i] = i + 1 and Pointer[7] = −1.Write program code for:• the global arrays and pointers, initialised as described• a function Insert(Item) that takes the first free node, stores Item in it and links it into the correct alphabetical position, returning False if there is no free node and True otherwise• a procedure OutputList() that outputs the items in list order. (Python shown; Java or VB.NET solutions are equally acceptable.) [10]
  2. (b) The towns Quetta, Bannu, Murree, Attock and Swat are inserted in that order. State the output of OutputList() and the final values of StartPointer and FreePointer. [3]
Show answer
  1. (a) NULL = -1SIZE = 8data = [""] * SIZEpointer = [i + 1 for i in range(SIZE)] # every node starts in the free listpointer[SIZE - 1] = NULLstart_pointer = NULLfree_pointer = 0def insert(item): global start_pointer, free_pointer if free_pointer == NULL: return False # list is full new_node = free_pointer free_pointer = pointer[free_pointer] # take the first free node data[new_node] = item previous = NULL current = start_pointer while current != NULL and data[current] < item: # find position previous = current current = pointer[current] pointer[new_node] = current if previous == NULL: # new first node start_pointer = new_node else: pointer[previous] = new_node return Truedef output_list(): current = start_pointer while current != NULL: print(data[current]) current = pointer[current]
    1 mark each for: arrays and pointers declared and initialised (free list linked); Insert returns False when FreePointer is null; takes node from FreePointer and updates FreePointer to the next free node; stores Item; traverses from StartPointer keeping previous and current; stops at end or at first item greater than or equal to Item; links new node to current; updates StartPointer when inserting at the front; otherwise updates previous node's pointer, returns True; OutputList follows pointers from StartPointer until null, outputting each item.
  2. (b) Attock, Bannu, Murree, Quetta, Swat. StartPointer = 3, FreePointer = 5.
    1 mark: correct output order. 1 mark: StartPointer 3 (Attock was stored in node 3). 1 mark: FreePointer 5.
Q16Abstract data types · Challenge · Paper 4 style · 13 marks
  1. (a) A binary search tree of exam scores is stored in three global arrays of 10 elements: Data, LeftPointer and RightPointer (null = −1). RootPointer starts at −1 and NextFree at 0; nodes are used in index order.Write program code for:• a procedure AddNode(Value) that stores Value in the next free node and links it into the tree (smaller values to the left, equal or larger to the right), outputting "Tree full" if all nodes are used• a recursive procedure InOrder(Node) that outputs the values in ascending order• a function Find(Value) that returns the number of comparisons made to find Value, or the negative of the number of comparisons made if Value is not in the tree. (Python shown; Java or VB.NET solutions are equally acceptable.) [10]
  2. (b) The scores 50, 30, 72, 18, 41, 66, 90, 35 are added in this order. State the output of InOrder(RootPointer), the value returned by Find(35) and the value returned by Find(70). [3]
Show answer
  1. (a) NULL = -1SIZE = 10data = [0] * SIZEleft = [NULL] * SIZEright = [NULL] * SIZEroot = NULLnext_free = 0def add_node(value): global root, next_free if next_free == SIZE: print("Tree full") return new = next_free data[new] = value next_free = next_free + 1 if root == NULL: root = new return current = root while True: if value < data[current]: if left[current] == NULL: left[current] = new return current = left[current] else: if right[current] == NULL: right[current] = new return current = right[current]def in_order(node): if node != NULL: in_order(left[node]) print(data[node], end=" ") in_order(right[node])def find(value): current = root comparisons = 0 while current != NULL: comparisons = comparisons + 1 if data[current] == value: return comparisons if value < data[current]: current = left[current] else: current = right[current] return -comparisons # negative: not found after this many comparisons
    1 mark each for: arrays and root/next-free initialised; AddNode checks for full; stores value and increments NextFree; sets root if tree empty; loop from root comparing value with node; follows left/right pointers; attaches new node where a null pointer is found; InOrder recursive with base case (node = null) visiting left, node, right; Find loop counting comparisons; correct returns for found and not found.
  2. (b) 18 30 35 41 50 66 72 90; Find(35) returns 4; Find(70) returns −3.
    1 mark each. Find(35): 50, 30, 41, 35. Find(70): 50, 72, 66, then a null pointer.
Q17Abstract data types · Challenge · Paper 3 style · 7 marks
  1. (a) Describe an algorithm (in steps, not pseudocode) to search a binary search tree for a given value. [4]
  2. (b) The values 10, 20, 30, 40, 50, 60, 70 are inserted in ascending order into an empty binary search tree. State the maximum number of comparisons needed to find a value, and the maximum if the same values had been inserted in the order 40, 20, 60, 10, 30, 50, 70. Explain the difference. [3]
Show answer
  1. (a) Start at the root node. Compare the search value with the value in the current node. If they are equal, the value is found – stop. If the search value is smaller, follow the left pointer; if larger, follow the right pointer. Repeat until the value is found or a null pointer is reached, in which case the value is not in the tree.
    1 mark each for: start at root; compare with current node and stop if equal; go left if smaller, right if larger; repeat until found or null pointer (not found).
  2. (b) Ascending order: 7 comparisons (the tree is a single chain, like a linked list). Second order: 3 comparisons (the tree is balanced, with 3 levels). A balanced tree halves the remaining nodes at each step (O(log n)); an unbalanced tree behaves like a linear search (O(n)).
    1 mark: 7. 1 mark: 3. 1 mark: explanation (balanced vs degenerate tree).
Q18Searching & Big O · Core · Paper 3 style · 5 marks
  1. (a) Describe how a linear search finds an item in a 1D array. [2]
  2. (b) State the condition that must be met before a binary search can be used. [1]
  3. (c) Explain why a binary search is usually faster than a linear search for a large array. [2]
Show answer
  1. (a) Start at the first element and compare each element in turn with the search item, until it is found or the end of the array is reached (item not present).
    1 mark: check each element in turn from the start. 1 mark: stop when found or end reached.
  2. (b) The data must be sorted (in order).
    B1.
  3. (c) Each comparison in a binary search discards half of the remaining items, so the number of comparisons grows as log₂ n; a linear search may have to check every item (n comparisons).
    1 mark: halves the search area each time. 1 mark: comparison with linear search's n comparisons / O(log n) vs O(n).
Q19Searching & Big O · Core · Paper 3 style · 4 marks
  1. (a) A sorted array contains 64 items. State the maximum number of comparisons needed to find an item using (i) a linear search (ii) a binary search. [2]
  2. (b) The array grows to 128 items. State how the maximum number of comparisons changes for each search. [2]
Show answer
  1. (a) (i) 64 (ii) 7
    1 mark each. Binary search: ⌊log₂ 64⌋ + 1 = 7.
  2. (b) Linear search doubles to 128; binary search increases by only one, to 8.
    1 mark each.
Q20Searching & Big O · Core · Paper 3 style · 5 marks
  1. (a) A bubble sort can be improved by using a Boolean flag. Describe how the flag improves the algorithm. [3]
  2. (b) State the best-case and worst-case time complexity of the improved bubble sort, describing the data that gives each. [2]
Show answer
  1. (a) The flag is set to FALSE at the start of each pass and set to TRUE whenever a swap is made. At the end of a pass, if no swap was made (flag still FALSE), the array is already sorted, so the algorithm stops early instead of doing the remaining passes.
    1 mark each: flag reset each pass; set when a swap happens; algorithm stops when a pass has no swaps.
  2. (b) Best case O(n) – the data is already sorted (one pass, no swaps). Worst case O(n²) – the data is in reverse order.
    1 mark each (complexity with matching data).
Q21Searching & Big O · Standard · Paper 3 style · 6 marks
  1. (a) The array Values[1:15] contains:3, 8, 11, 17, 22, 26, 31, 38, 42, 47, 53, 59, 64, 70, 78A binary search uses Mid ← (Low + High) DIV 2, starting with Low = 1 and High = 15. State the values of Low, High and Mid at each step when searching for 59. [3]
  2. (b) State the values of Mid in order when searching for 20, and the number of comparisons made before the search reports that 20 is not present. [3]
Show answer
  1. (a) Step 1: Low 1, High 15, Mid 8 (38 < 59). Step 2: Low 9, High 15, Mid 12 – Values[12] = 59, found.
    1 mark: first step correct. 1 mark: Low becomes 9. 1 mark: Mid 12, found.
  2. (b) Mid = 8, 4, 6, 5; 4 comparisons.
    Steps: (1, 15) Mid 8 → 38 > 20; (1, 7) Mid 4 → 17 < 20; (5, 7) Mid 6 → 26 > 20; (5, 5) Mid 5 → 22 > 20; High becomes 4 < Low, so not found. 2 marks for 8, 4, 6, 5 (1 mark for three correct); 1 mark for 4 comparisons.
Q22Searching & Big O · Standard · Paper 3 style · 6 marks
  1. (a) State the Big O time complexity of each algorithm in the worst case.(i) linear search (ii) binary search (iii) bubble sort (iv) insertion sort [4]
  2. (b) Explain what Big O notation describes, and state the space complexity of an insertion sort carried out in place. [2]
Show answer
  1. (a) (i) O(n) (ii) O(log n) (iii) O(n²) (iv) O(n²)
    1 mark each.
  2. (b) Big O describes how the time (or memory) needed by an algorithm grows as the size of the input n increases, in the worst case/order of growth (ignoring constants). In-place insertion sort has space complexity O(1) (only a fixed number of extra variables).
    1 mark: growth of time/space with input size. 1 mark: O(1).
Q23Searching & Big O · Standard · Paper 3 style · 6 marks
  1. (a) An insertion sort is used to put this array into ascending order:29, 13, 41, 7, 22, 35Write the contents of the array after each pass of the outer loop (each pass inserts the next element into the sorted part). [5]
  2. (b) Explain why insertion sort is efficient for an array that is already almost sorted. [1]
Show answer
  1. (a) Pass 1: 13, 29, 41, 7, 22, 35Pass 2: 13, 29, 41, 7, 22, 35Pass 3: 7, 13, 29, 41, 22, 35Pass 4: 7, 13, 22, 29, 41, 35Pass 5: 7, 13, 22, 29, 35, 41
    1 mark for each correct pass.
  2. (b) Each new element needs only one (or very few) comparisons and no (or few) shifts, so the time is close to O(n).
    B1.
Q24Searching & Big O · Challenge · Paper 3 style · 8 marks
  1. (a) Write a pseudocode function BinarySearch(SearchItem : STRING) RETURNS INTEGER that searches the global sorted array Names[1:500] and returns the index of SearchItem, or −1 if it is not found. Use an iterative (not recursive) method. [7]
  2. (b) State the maximum number of comparisons with Names[Mid] your function makes for an array of 500 names. [1]
Show answer
  1. (a) FUNCTION BinarySearch(SearchItem : STRING) RETURNS INTEGER DECLARE Low, High, Mid : INTEGER Low ← 1 High ← 500 WHILE Low <= High Mid ← (Low + High) DIV 2 IF Names[Mid] = SearchItem THEN RETURN Mid ELSE IF Names[Mid] < SearchItem THEN Low ← Mid + 1 ELSE High ← Mid - 1 ENDIF ENDIF ENDWHILE RETURN -1ENDFUNCTION
    1 mark each for: Low and High initialised to 1 and 500; loop while Low <= High; Mid calculated with DIV; return Mid when found; Low ← Mid + 1 when Names[Mid] < SearchItem; High ← Mid − 1 otherwise; return −1 after the loop.
  2. (b) 9
    B1 2⁸ = 256 < 500 < 512 = 2⁹, so at most ⌊log₂ 500⌋ + 1 = 9.
Q25Searching & Big O · Challenge · Paper 3 style · 5 marks
  1. (a) An algorithm with time complexity O(n²) takes 2 seconds to process 1000 records on a server in Islamabad. Estimate the time to process 5000 records on the same server. [2]
  2. (b) A linear search of 2000 records takes 0.4 ms in the worst case. Estimate the worst-case time for 10 000 records. [1]
  3. (c) A binary search needs at most 10 comparisons for an array of 1000 items. State the maximum number of comparisons for 1 000 000 items and explain your answer. [2]
Show answer
  1. (a) 50 seconds
    M1 records × 5, so time × 5² = 25. A1 2 × 25 = 50 s.
  2. (b) 2 ms
    B1 O(n): 0.4 × 5 = 2 ms.
  3. (c) 20 comparisons. Binary search is O(log n): multiplying n by 1000 ≈ 2¹⁰ adds about 10 more halvings (log₂ 1 000 000 ≈ 19.9, so ⌊19.9⌋ + 1 = 20).
    1 mark: 20. 1 mark: explanation based on log₂ n / each doubling adds one comparison.
Q26Searching & Big O · Challenge · Paper 4 style · 13 marks
  1. (a) Write program code for:• a function InsertionSort(Items) that sorts a list/array of integers into ascending order in place and returns the number of comparisons made between Items[j] and the value being inserted• a function BinarySearch(Items, Target) that returns the index of Target in the sorted list (indices start at 0), or −1 if not present. (Python shown; Java or VB.NET solutions are equally acceptable.) [9]
  2. (b) The main program sorts the list 8611, 4692, 7027, 3500, 8126, 6050, 5100, 7788 and then searches for 7027 and for 6000. State the sorted list, the number of comparisons returned by InsertionSort, and the two values returned by BinarySearch. [4]
Show answer
  1. (a) def insertion_sort(items): comparisons = 0 for i in range(1, len(items)): key = items[i] j = i - 1 while j >= 0: comparisons = comparisons + 1 if items[j] > key: items[j + 1] = items[j] # shift larger item right j = j - 1 else: break items[j + 1] = key return comparisonsdef binary_search(items, target): low = 0 high = len(items) - 1 while low <= high: mid = (low + high) // 2 if items[mid] == target: return mid elif items[mid] < target: low = mid + 1 else: high = mid - 1 return -1
    InsertionSort (5): outer loop from the second element; store key; inner loop moving left while previous element is larger; shift elements right and insert key in the gap; count each comparison and return the count. BinarySearch (4): low/high initialised; loop while low <= high with mid calculated by integer division; return mid when found; move low or high correctly and return −1 after loop.
  2. (b) 3500, 4692, 5100, 6050, 7027, 7788, 8126, 8611; 20 comparisons; BinarySearch(…, 7027) returns 4; BinarySearch(…, 6000) returns −1.
    1 mark each.
Q27Recursion · Core · Paper 3 style · 6 marks
  1. (a) Explain what is meant by a recursive subroutine and state the two types of case it must have. [3]
  2. (b) FUNCTION Mystery(N : INTEGER) RETURNS INTEGER IF N = 0 THEN RETURN 0 ELSE RETURN (N MOD 10) + Mystery(N DIV 10) ENDIFENDFUNCTIONState the value returned by Mystery(4073), the number of times Mystery is called in total, and what the function calculates. [3]
Show answer
  1. (a) A subroutine that is defined in terms of itself / calls itself. It must have a base case (a stopping condition that returns without a further call) and a general (recursive) case that calls itself with a value that moves towards the base case.
    1 mark: calls itself. 1 mark: base case. 1 mark: general case.
  2. (b) Returns 14; 5 calls (4073, 407, 40, 4, 0); it calculates the sum of the digits of N.
    1 mark each. 3 + 7 + 0 + 4 + 0 = 14.
Q28Recursion · Standard · Paper 3 style · 5 marks
  1. (a) PROCEDURE Show(N : INTEGER) IF N > 0 THEN CALL Show(N DIV 2) OUTPUT N MOD 2 ENDIFENDPROCEDUREThe procedure outputs each value on the same line with no spaces. State the output of CALL Show(13) and explain why the digits appear in this order. [3]
  2. (b) State what the procedure does, and the output of CALL Show(0). [2]
Show answer
  1. (a) 1101. Each call makes its recursive call before outputting, so the deepest call (N = 1) outputs first as the calls unwind: 1 (N = 1), 1 (N = 3), 0 (N = 6), 1 (N = 13).
    1 mark: 1101. 1 mark: output happens after the recursive call / on unwinding. 1 mark: correct link of digits to calls.
  2. (b) It outputs the binary equivalent of N. Show(0) outputs nothing (the base case is reached immediately).
    1 mark each.
Q29Recursion · Challenge · Paper 3 style · 8 marks
  1. (a) FUNCTION Power(B : INTEGER, E : INTEGER) RETURNS INTEGER DECLARE Half : INTEGER IF E = 0 THEN RETURN 1 ENDIF IF E MOD 2 = 0 THEN Half ← Power(B, E DIV 2) RETURN Half * Half ELSE RETURN B * Power(B, E - 1) ENDIFENDFUNCTIONList the calls made when Power(3, 10) is evaluated, and state the value returned. [4]
  2. (b) Explain how the stack is used when Power(3, 10) runs, and state the maximum number of stack frames for Power at any one time. [3]
  3. (c) Give one reason why an iterative solution might be preferred to a recursive one. [1]
Show answer
  1. (a) Power(3, 10) → Power(3, 5) → Power(3, 4) → Power(3, 2) → Power(3, 1) → Power(3, 0). Value returned: 59049.
    2 marks: all six calls in order (1 mark for five correct). 1 mark: unwinding values 1, 3, 9, 81, 243, 59049 (or equivalent working). 1 mark: 59049.
  2. (b) Each call pushes a stack frame holding the return address, parameters (B, E) and local variable Half. Frames are popped in reverse order as each call returns its value to the caller. Maximum 6 frames (calls with E = 10, 5, 4, 2, 1, 0).
    1 mark: frame with return address/parameters/local variables pushed for each call. 1 mark: popped in reverse order on return (LIFO). 1 mark: 6.
  3. (c) Recursion uses more memory (a stack frame per call) and could cause stack overflow for deep recursion; it can also be slower because of the call overheads.
    B1.
Q30Recursion · Challenge · Paper 4 style · 10 marks
  1. (a) A child can climb a staircase by taking either 1 step or 2 steps at a time. The number of different ways to climb N steps is: Ways(1) = 1, Ways(2) = 2, and Ways(N) = Ways(N − 1) + Ways(N − 2) for N > 2.Write program code for a recursive function Ways(N) and an iterative function WaysIterative(N) that give the same results. (Python shown; Java or VB.NET solutions are equally acceptable.) [6]
  2. (b) State the values returned for N = 5 and N = 10. [2]
  3. (c) The recursive function makes 109 calls in total to calculate Ways(10). Explain why it is very slow for large N and how the iterative version avoids this. [2]
Show answer
  1. (a) def ways(n): if n == 1: return 1 # base case: one step left if n == 2: return 2 # base case: 1+1 or 2 return ways(n - 1) + ways(n - 2) # general casedef ways_iterative(n): if n <= 2: return n a, b = 1, 2 for _ in range(n - 2): a, b = b, a + b return b
    Recursive (3): base case N = 1 returns 1; base case N = 2 returns 2; general case returns Ways(N − 1) + Ways(N − 2). Iterative (3): handles N ≤ 2; keeps the last two values; loop updates them N − 2 times and returns the result.
  2. (b) Ways(5) = 8; Ways(10) = 89
    1 mark each.
  3. (c) Each call makes two further calls, so the same values (e.g. Ways(3)) are recalculated many times and the number of calls grows roughly exponentially; the iterative version calculates each value once (O(n)) by keeping the previous two results.
    1 mark: repeated recalculation / exponential growth. 1 mark: iterative computes each value once / linear time.

Similar questions

Questions that test the same subtopic in different years, grouped so you can work on one skill at a time.

References only: find each question in its paper (official link where available, otherwise through your school).

Abstract data types 6 questions · 6/9 sessions
Searching & Big O 3 questions · 3/9 sessions
Recursion 3 questions · 3/9 sessions

Subtopics

Our analysis of how often each subtopic was examined and how many marks it carried, 2022 to May/June 2026.

SubtopicMarks indexedSessions
Abstract data types 516/9
Searching & Big O 243/9
Recursion 93/9

← All Computer Science topics