Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (9618)

7 questions · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
1
9618/42/M/J/26 · Question 1May/June 2026Paper 4218 marks
Loading question…
Question text

A program stores nine lower‑case strings in a 1D array TheData. The following strings are
1
stored in the order shown:
laptop
tablet
printer
computer
processor
monitor
memory
cable
touchscreen
(a) Write program code to declare the array TheData in the main program. Store the given
strings in the array.
Save your program as Question1_J26.
Copy and paste the program code into 1(a) in the evidence document.
[2]
The procedure FindLongestShortest():
(b) (i)
•
takes an array of strings as a parameter
•
finds and outputs the longest string in the array of strings
•
finds and outputs the shortest string in the array of strings.
All outputs must have appropriate messages. Do not use in‑built functions to find the
longest and shortest string in the array. You may use an in‑built length function to find
the length of a string.
You can assume that there will only be one string that has the shortest length.
You can assume that there will only be one string that has the longest length.
Write program code for FindLongestShortest()
Save your program.
Copy and paste the program code into 1(b)(i) in the evidence document.
[5]
© Cambridge University Press & Assessment 2026
Write program code to extend the main program to call FindLongestShortest()
(ii)
with TheData
Save your program.
Copy and paste the program code into 1(b)(ii) in the evidence document.
[1]
(iii)
Test your program
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(b)(iii) in the evidence document.
[1]
The function InsertionSort():
(c) (i)
•
takes an array of strings as a parameter
•
sorts the strings in the array into ascending alphabetical order using an insertion sort
•
returns the sorted array.
The function must work for an array of any length. Do not use an in‑built function to sort
the array.
Write program code for InsertionSort()
Save your program.
Copy and paste the program code into 1(c)(i) in the evidence document.
[5]
(ii)
Write program code to extend the main program to perform the following in the
order shown:
•
output the string "Before sorting"
•
output the contents of the array TheData
•
call InsertionSort() with TheData as a parameter
•
output the string "After sorting"
•
output the contents of the returned sorted array.
Save your program.
Copy and paste the program code into 1(c)(ii) in the evidence document.
[3]
(iii)
Test your program
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(c)(iii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026

2
9618/43/M/J/26 · Question 3May/June 2026Paper 4325 marks
Loading question…
Question text

3
A binary tree is designed to store up to 100 positive integers. The binary tree stores data in
ascending numerical order.
The data in the binary tree is created as a global 2D array with the identifier BinaryTree
Each node has three values:
•
a pointer to the array index with the left node
•
the integer data
•
a pointer to the array index with the right node.
A null pointer is represented by the integer –1. All nodes are initialised with the null data –1
The table has example data for a binary tree:
index
left pointer
data
right pointer
0
1
20
2
1
4
10
–1
2
3
26
–1
3
–1
22
–1
4
–1
8
–1
The binary tree has two global pointers:
•
TreeRootPointer, initialised to –1, stores the index of the root node of the binary tree
•
FirstFreeNodePointer, initialised to 0, stores the index of the first node that does not
store any data.
Data cannot be removed or replaced from the tree. FirstFreeNodePointer is incremented
each time a node is added to the binary tree.
(a) The main program initialises all 100 nodes, TreeRootPointer
and FirstFreeNodePointer
Write program code for the main program.
Save your program as Question3_J26.
Copy and paste the program code into 3(a) in the evidence document.
[3]
© Cambridge University Press & Assessment 2026
(b) The procedure AddNode():
•
takes an integer parameter to be stored as the data in a new node
•
stores the data in the first element and updates the root pointer when the tree is empty
•
outputs "The tree is full" if the tree is full
•
stores the data in the next free node if the tree is not full, then:
◦
checks if the data is less than or greater than the root node
◦
follows the left or right pointer(s) repeatedly until it reaches the appropriate
leaf node
◦
updates the appropriate parent node pointer to the new node.
Write program code for AddNode()
Save your program.
Copy and paste the program code into 3(b) in the evidence document.
[7]
(c) The procedure OutputArray() outputs the content of the first 10 elements in the binary
tree. Each node is output on one line in the format:
<left pointer> <data> <right pointer>
For example:
1
20
4
Write program code for OutputArray()
Save your program.
Copy and paste the program code into 3(c) in the evidence document.
[2]
© Cambridge University Press & Assessment 2026
(d) (i)
Write program code to extend the main program to:
•
take 10 integers as input
•
store each input in the binary tree in the order they are entered using the
appropriate procedure
•
output the content of the binary tree using OutputArray()
Save your program.
Copy and paste the program code into 3(d)(i) in the evidence document.
[3]
(ii)
Test your program. Enter the following numbers in the order shown:
50
26
120
236
2
16
67
49
165
15
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 3(d)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(e) An in‑order tree traversal outputs the contents of the binary tree in ascending numerical
order by:
•
visiting the left node
•
output the data
•
visiting the right node.
For example, an in‑order traversal on the following binary tree will output: 5 12 18 20 26
20
12
26
5
18
The recursive procedure InOrder():
(i)
•
takes the root pointer as a parameter
•
makes a recursive call with the left pointer as the parameter if the left pointer is
not null
•
outputs the data at the parameter
•
makes a recursive call with the right pointer as the parameter if the right pointer is
not null.
Write program code for InOrder()
Save your program.
Copy and paste the program code into 3(e)(i) in the evidence document.
[7]
Write program code to extend the main program to call InOrder()
(ii)
Save your program.
Copy and paste the program code into 3(e)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(iii)
Test your program. Enter the following numbers in the order given:
50
26
120
236
2
16

3
9618/41/M/J/25 · Question 3May/June 2025Paper 4130 marks
Loading question…
Question text

3
A program stores data in a binary tree that is designed using Object‑Oriented Programming
(OOP).
The tree stores data in ascending numerical order, for example:
30
20
45
1
21
33
63
The class Node stores data about the nodes.
Node
NodeData : Integer
stores the node’s integer data
LeftNode : Node
stores the node that is stored to the left of the current
node, or a null value if there is no node to the left
RightNode : Node
stores the node that is stored to the right of the current
node, or a null value if there is no node to the right
Constructor()
initialises NodeData to its parameter value; initialises
LeftNode and RightNode to a null value
GetLeft()
returns LeftNode
GetRight()
returns RightNode
GetData()
returns NodeData
SetLeft()
takes an object of type Node as a parameter and stores
it in LeftNode
SetRight()
takes an object of type Node as a parameter and stores
it in RightNode
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J25.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[4]
(ii)
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
The method SetLeft() takes an object of type Node as a parameter. The method
(iii)
stores the parameter in the attribute LeftNode
The method SetRight() takes an object of type Node as a parameter. The method
stores the parameter in the attribute RightNode
Write program code for SetLeft() and SetRight()
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[3]
(b) Write the main program to declare five objects of type Node :
•
Node 1 with the data 10
•
Node 2 with the data 20
•
Node 3 with the data 5
•
Node 4 with the data 15
•
Node 5 with the data 7
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The class Tree stores the tree.
Tree
FirstNode : Node
stores the root node in the tree
Constructor()
initialises FirstNode to its parameter value
GetRootNode()
returns the node stored in FirstNode
Insert()
stores its parameter node in the correct position in the
tree
Write program code to declare the class Tree and its constructor.
(i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[2]
Write program code for GetRootNode()
(ii)
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[1]
The method Insert() takes a node as a parameter and then searches the tree to find
(iii)
the position to insert the new node by:
•
checking whether the node’s data is less than or greater than the root node’s data
•
moving to the left node if the data is less than the root node’s data
•
moving to the right node if the data is greater than or equal to the root node’s data
•
repeating until the final position of the node is found and stores the node in that
position.
Write program code for Insert()
Save your program.
Copy and paste the program code into part 3(c)(iii) in the evidence document.
[6]
(d) The recursive procedure OutputInOrder() outputs the data stored in the binary tree in
ascending numerical order.
The procedure takes a Node object as a parameter and then:
•
checks if the left node is null. If there is a left node, the function calls itself with the left
node
•
outputs the data of the current node
•
checks if the right node is null. If there is a right node, the function calls itself with the
right node.
Write program code for OutputInOrder()
Save your program.
Copy an

Advertisement
4
9618/43/M/J/25 · Question 2May/June 2025Paper 4320 marks
Loading question…
Question text

A program sorts the data in the 1D array DataArray and searches DataArray for specific
2
values.
DataArray stores 14 integer values and is declared local to the main program.
(a) Write program code to create DataArray and initialise it with the following data values in the
order they are written:
0
3
4
56
67
44
43
32
31
345
45
6
54
1
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
[1]
(b) The function InsertionSort() takes an array of integers as a parameter. The function
sorts the data in the array into ascending numerical order using an insertion sort. The function
returns the sorted array.
Write program code for InsertionSort()
You must not use any inbuilt sorting functions for your programming language.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[5]
(c) The procedure OutputArray() takes an array of integers as a parameter. The procedure
outputs each element in the array from the first element to the last element. The output is on
one line with a space between each number.
An example output is:
"0 3 4 56 67 44 43 32 31 345 45 6 54 1"
Write program code for OutputArray()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) (i)
The main program needs extending to:
•
output the content of the unsorted array using OutputArray()
•
sort the array using InsertionSort()
•
output the content of the sorted array using OutputArray()
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(d)(ii) in the evidence document.
[1]
(e) The function Search() performs a binary search to find ItemToFind in DataArray
The function takes two parameters:
•
DataArray, an array of integers
•
ItemToFind, an integer to find in DataArray
The function returns:
•
the index of ItemToFind if it is in DataArray
•
–1 if ItemToFind is not in DataArray
Write program code for Search()
You must not use any inbuilt searching functions for your programming language.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[6]
The main program needs extending to call Search() with the sorted array four times:
(f)
(i)
•
the first time to find the index of the integer 0
•
the second time to find the index of the integer 345
•
the third time to find the index of the integer 67
•
the fourth time to find the index of the integer 2
If the integer is found in the array, output an appropriate message that includes the index.
If the integer is not found, output that it was not found.
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(f)(ii) in the evidence document.
[1]

5
9618/42/M/J/24 · Question 2May/June 2024Paper 4230 marks
Loading question…
Question text

2
A binary tree stores data in ascending order. For example:
15
8
19
3
10
A computer program stores integers in a binary tree in ascending order. The program uses
Object‑Oriented Programming (OOP).
The binary tree is stored as a 1D array of nodes. Each node contains a left pointer, a data value
and a right pointer.
The class Node stores the data about a node.
Node
LeftPointer : INTEGER
stores the index of the node to the left in the binary tree
Data : INTEGER
stores the node’s data
RightPointer : INTEGER
stores the index of the node to the right in the binary tree
Constructor()
initialises Data to its parameter value
initialises LeftPointer and RightPointer to −1
GetLeft()
returns the left pointer
GetRight()
returns the right pointer
GetData()
returns the data value
SetLeft()
assigns the parameter to the left pointer
SetRight()
assigns the parameter to the right pointer
SetData()
assigns the parameter to the data
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J24.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The get methods GetLeft(), GetRight()and GetData()each return the relevant
(ii)
attribute.
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
The set methods SetLeft(), SetRight()and SetData()each take a parameter and
(iii)
then store this in the relevant attribute.
Write program code for the three set methods.
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[3]
(b) The class TreeClass stores the data about the binary tree.
TreeClass
Tree[0:19] : Node
an array of 20 elements of type Node
FirstNode : INTEGER
stores the index of the first node in the tree
NumberNodes : INTEGER
stores the quantity of nodes in the tree
Constructor()
initialises FirstNode to −1 and NumberNodes to 0
initialises each element in Tree to a Node object with the
data value of −1
InsertNode()
takes a Node object as a parameter, inserts it in the array
and updates the pointer for its parent node
OutputTree()
outputs the left pointer, data and right pointer of each
node in Tree
Nodes cannot be deleted from this tree.
Write program code to declare the class TreeClass and its constructor.
(i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[4]
The method InsertNode()takes a Node object, NewNode, as a parameter and inserts
(ii)
it into the array Tree.
InsertNode()first checks if the tree is empty.
If the tree is empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
increments NumberNodes
•
stores 0 in FirstNode.
If the tree is not empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
accesses the data in the array Tree at index FirstNode and compares it to the
data in NewNode
•
repeatedly follows the pointers until the correct position for NewNode is found
•
once the position is found, InsertNode()sets the left or right pointer of its parent
node
•
increments NumberNodes.
Write program code for InsertNode().
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[6]
The method OutputTree()outputs the left pointer, the data and the right pointer for
(iii)
each node that has been inserted into the tree. The outputs are in the order they are
saved in the array.
If there are no nodes in the array, the procedure outputs ‘No nodes’.
Write program code for OutputTree().
Save your program.
Copy and paste the progra

6
9618/42/M/J/24 · Question 3May/June 2024Paper 4223 marks
Loading question…
Question text

3
A program sorts an array of integers and searches the array for a particular value.
(a) The array of integers, NumberArray, stores the following data in the order given:
100
85
644
22
15
8
1
The array is declared and initialised local to the main program.
Write program code to declare and initialise the array.
Save your program as Question3_J24.
Copy and paste the program code into part 3(a) in the evidence document.
[1]
(b) (i)
The following recursive pseudocode function sorts the array into ascending order using
an insertion sort and returns the sorted array.
DECLARE LastItem : INTEGER
DECLARE CheckItem : INTEGER
DECLARE LoopAgain : BOOLEAN
FUNCTION RecursiveInsertion(IntegerArray : ARRAY[] OF INTEGER,
NumberElements : INTEGER) RETURNS ARRAY[] OF INTEGER
IF NumberElements <= 1 THEN
RETURN IntegerArray
ELSE
CALL RecursiveInsertion(IntegerArray, NumberElements - 1)
LastItem
IntegerArray[NumberElements - 1]
CheckItem
NumberElements - 2
ENDIF
LoopAgain
TRUE
IF CheckItem < 0 THEN
LoopAgain
FALSE
ELSE
IF IntegerArray[CheckItem] < LastItem THEN
LoopAgain
FALSE
ENDIF
ENDIF
WHILE LoopAgain
IntegerArray[CheckItem + 1]
IntegerArray[CheckItem]
CheckItem
CheckItem - 1
IF CheckItem < 0 THEN
LoopAgain
FALSE
ELSE
IF IntegerArray[CheckItem] < LastItem THEN
LoopAgain
FALSE
ENDIF
ENDIF
ENDWHILE
IntegerArray[CheckItem + 1]
LastItem
RETURN IntegerArray
ENDFUNCTION
Write the program code for the pseudocode function RecursiveInsertion().
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[4]
(ii)
Amend the main program to:
•
call RecursiveInsertion()with the initialised array NumberArray and the
number of elements as parameters
•
output the word ‘Recursive’
•
output the content of the returned array.
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[2]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(b)(iii) in the evidence document.
[1]
(c) The function RecursiveInsertion()can be changed to use iteration instead of recursion.
Write program code for the function IterativeInsertion()to perform the same
(i)
processes as RecursiveInsertion()but using iteration instead of recursion.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[4]
(ii)
Write program code to amend the main program to also:
•
call IterativeInsertion()with the original initialised array NumberArray
•
output the word ‘iterative’
•
output the content of the returned array.
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[1]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(c)(iii) in the evidence document.
[1]
(d) The recursive function BinarySearch()takes the parameters:
•
IntegerArray – an array of integers
•
First – the index of the first array element
•
Last – the index of the last array element
•
ToFind – an integer to search for in the array.
The function uses recursion to perform a binary search for ToFind in IntegerArray.
The function returns the index where ToFind is stored or returns −1 if ToFind is not in the
array.
Write program code for the recursive function BinarySearch().
(i)
Save your program.
Copy and paste the program code into part 3(d)(i) in the evidence document.
[6]
(ii)
Write program code to amend the main program to:
•
call BinarySearch()with the sorted array and the integer 644 as the search value
•
output ‘Not found’ if 644 is not found in the array
•
output the index if 644 is found in the array.
Save your program.
Copy and paste the program code into part 3(d)(ii) in the evidence document.
[2]
(iii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 3(d)(iii) in the evidence document.
[1]

7
9618/42/M/J/22 · Question 2May/June 2022Paper 4223 marks
Loading question…
Question text

2
A 2D array stores data entered by a user.
(a) The main program declares a 2D array of 10 by 10 integer elements.
The array is initialised with a random number between 1 and 100 in each element.
Write program code for the main program.
Save your program as Question2_J22.
Copy and paste the program code into part 2(a) in the evidence document.
[4]
(b) The following bubble sort pseudocode algorithm sorts the data in the first dimension of the 2D
array into ascending numerical order.
10
ArrayLength
0 TO ArrayLength - 1
FOR X
0 TO ArrayLength - 2
FOR Y
0 TO ArrayLength - Y - 2
FOR Z
IF ArrayData[X, Z] > ArrayData[X, Z + 1] THEN
ArrayData[X, Z]
TempValue
ArrayData[X, Z+1]
ArrayData[X, Z]
TempValue
ArrayData[X, Z + 1]
ENDIF
NEXT Z
NEXT Y
NEXT X
(i)
Amend your main program by writing program code to implement the bubble sort
algorithm after the initialisation of the array elements.
You must not use any built-in sorting functions for your programming language.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[5]
(ii)
Write program code for a procedure to output all the values in the 2D array. The values
should be output as a 2D grid, with values in rows and columns.
Call the procedure before and after your bubble sort code.
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[3]
(iii)
Test your program.
Take a screenshot to show the output.
Copy and paste the screenshot into part 2(b)(iii) in the evidence document.
[1]
(c) The following pseudocode function uses recursion to perform a binary search in the first row
of the array, for the value SearchValue in the array SearchArray.
The function returns −1 if the item was not found, or it returns the index where it is found.
There are six incomplete statements.
FUNCTION BinarySearch(SearchArray, Lower, Upper, SearchValue)RETURNS
INTEGER
IF Upper >= Lower THEN
(Lower + (Upper – 1)) DIV …
Mid
IF SearchArray[0, Mid] = … THEN
RETURN …
ELSE
IF SearchArray[0, Mid] > SearchValue THEN
RETURN BinarySearch(SearchArray, … , Mid – 1,
SearchValue)
ELSE
RETURN BinarySearch(SearchArray, Mid + 1, … ,
SearchValue)
ENDIF
ENDIF
ENDIF
RETURN …
ENDFUNCTION
Note: the arithmetic operator DIV performs integer division, e.g. the result of 10 DIV 3 will be 3.
(i)
Write program code for the recursive function BinarySearch().
Save your program.
Copy and paste the program code into part 2(c)(i) in the evidence document.
[8]
(ii)
In the main program, test the function BinarySearch() twice, outputting the returned
value each time.
One test should be for a number that is in the first line of the array.
One test should be for a number that is not in the first line of the array.
Take a screenshot to show the output.
Copy and paste the screenshot into part 2(c)(ii) in the evidence document.
[2]

Advertisement