9618/41 · May/June 2025 · Worked solutions

Step-by-step solutions based on the mark scheme (not a mark-scheme dump).

Question 1

Paper page 2 for Q1
Original paper page 2

Part 1(a) — solution

  1. (global) Declaration of 1D array Queue, 20 elements initialised with –1
  2. (global) HeadPointer and TailPointer initialised to –1, NumberItems initialised with 0

Visual Basic (VB.NET)

Dim Queue(20) As Integer
 Dim HeadPointer As Integer
 Dim TailPointer As Integer
 Dim NumberItems As Integer
 For x = 0 To 19
    Queue(x) = -1
 Next
 HeadPointer = -1
 TailPointer = -1
 NumberItems = 0

Python

Queue = [-1 for x in range(20)]
 HeadPointer = -1
 TailPointer = -1
 NumberItems = 0

Mark points

  • (global) Declaration of 1D array Queue, 20 elements initialised with –1
  • (global) HeadPointer and TailPointer initialised to –1, NumberItems initialised with 0

Part 1(b) — solution

  1. Function header (and close) taking 1 (integer) parameter and returning a Boolean value in all cases
  2. Checking if queue is full (NumberItems = 20) and returning FALSE
  3. Checking if queue is empty (NumberItems = 0) then and updating TailPointer and HeadPointer appropriately
  4. Incrementing TailPointer and NumberItems in appropriate place …
  5. … looping back to 0 for TailPointer if at end of structure
  6. Storing parameter in Queue[TailPointer] (after increment) and returning TRUE

Visual Basic (VB.NET)

Function Enqueue(InputData)
    If NumberItems >= 20 Then
        Return False
    End If

      If TailPointer <= -1 Then
         TailPointer = 0
         HeadPointer = 0
         Queue(TailPointer) = InputData

 University Press & Assessment 2025                       Page 10 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

    Else
       TailPointer = TailPointer + 1
       If TailPointer = 20 Then
            TailPointer = 0
       End If
       Queue(TailPointer) = InputData
    End If
    NumberItems = NumberItems + 1
    Return True
 End Function

Python

def Enqueue(InputData):
      global Queue
      global HeadPointer
      global TailPointer
      global NumberItems
      if NumberItems >= 20:
          return False

        if TailPointer <= -1:
            TailPointer = 0
            HeadPointer = 0
            Queue[TailPointer] = InputData
        else:
            TailPointer = TailPointer + 1
            if TailPointer == 20:
                TailPointer = 0

            Queue[TailPointer] = InputData
        NumberItems +=1
        return True

Mark points

  • Function header (and close) taking 1 (integer) parameter and returning a Boolean value in all cases
  • Checking if queue is full (NumberItems = 20) and returning FALSE
  • Checking if queue is empty (NumberItems = 0) then and updating TailPointer and HeadPointer appropriately
  • Incrementing TailPointer and NumberItems in appropriate place …
  • … looping back to 0 for TailPointer if at end of structure
  • Storing parameter in Queue[TailPointer] (after increment) and returning TRUE

Part 1(c) — solution

  1. Calling Enqueue() with 1 to 25 (inclusive) in order
  2. … storing/using return value in selection …outputting Successful with integer and outputting Unsuccessful with

Visual Basic (VB.NET)

Dim ReturnValue As Boolean
 For x = 1 To 25
    ReturnValue = Enqueue(x)
    If ReturnValue = True Then
        Console.WriteLine(x & "Successful " )
    Else
        Console.WriteLine(x & "Unsuccessful ")
    End If
 Next x

Python

for X in range(1, 26):
      ReturnValue = Enqueue(X)
      if ReturnValue == True:
          print(x,"Successful")
      else:
          print(x,"Unsuccessful")

Mark points

  • Calling Enqueue() with 1 to 25 (inclusive) in order
  • … storing/using return value in selection …outputting Successful with integer and outputting Unsuccessful with

Part 1(d) — solution

  1. Dequeue() header (and close) and checking if queue is empty (NumberItems = 0) and returning –1
  2. Returning data at HeadPointer
  3. Incrementing HeadPointer …
  4. … and catching if = 20 to return to 0
  5. Decrementing NumberItems
  6. Resetting HeadPointer and TailPointer when queue is empty

Visual Basic (VB.NET)

Function Dequeue()
    Dim ReturnValue As Integer
    If NumberItems <= 0 Then
         Return -1
    Else
        ReturnValue = Queue(HeadPointer)
        HeadPointer = HeadPointer + 1
        If HeadPointer >= 20 Then
           HeadPointer = 0
        End If
        NumberItems = NumberItems – 1
        If NumberItems = 0 Then
           HeadPointer = -1
           TailPointer = -1

 University Press & Assessment 2025                       Page 14 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

       End If
       Return ReturnValue
    End If
 End Function

Python

def Dequeue():
      global Queue
      global HeadPointer
      global TailPointer
      global NumberItems
      if NumberItems <= 0:
          return -1
      else:
          ReturnValue = Queue[HeadPointer]
          HeadPointer +=1
          if HeadPointer >= 20:
              HeadPointer = 0
          NumberItems -=1
          if NumberItems == 0:
              HeadPointer = -1
              TailPointer = -1
          return ReturnValue

Mark points

  • Dequeue() header (and close) and checking if queue is empty (NumberItems = 0) and returning –1
  • Returning data at HeadPointer
  • Incrementing HeadPointer …
  • … and catching if = 20 to return to 0
  • Decrementing NumberItems
  • Resetting HeadPointer and TailPointer when queue is empty

Part 1(e)(i) — solution

  1. Calling Dequeue() twice
  2. … outputting return value from both calls

Visual Basic (VB.NET)

Dim NextValue As Integer
 NextValue = Dequeue()
 Console.WriteLine(NextValue)
 NextValue = Dequeue()
 Console.WriteLine(NextValue)

Python

NextValue = Dequeue()
 print(NextValue)
 NextValue = Dequeue()
 print(NextValue)

Mark points

  • Calling Dequeue() twice
  • … outputting return value from both calls

Part 1(e)(ii) — solution

  1. 1 to 20 with Successful

Mark points

  • 1 to 20 with Successful

Question 2

Paper page 4 for Q2
Original paper page 4

Part 2(a) — solution

  1. Function header (and end)
  2. Prompt to enter filename and reading input
  3. Opening the file (to read) and closing the file in an appropriate place
  4. Looping until EOF …
  5. … reading each line in the file …
  6. … (removing line break and) inserting in array
  7. Returning populated array
  8. Exception handling try catch with appropriate output

Visual Basic (VB.NET)

Function ReadData()
    Dim DataList(100) As String
    Console.WriteLine("Enter the filename")
    Dim FileName As String = Console.ReadLine()
    NumberItems = 0

      Try
       Dim FileReader As New System.IO.StreamReader(FileName)
       While Not FileReader.EndOfStream
           DataList(NumberItems) = FileReader.ReadLine()
           NumberItems = NumberItems + 1
       End While
       FileReader.Close()
    Catch ex As Exception
       Console.WriteLine("Cannot open or read from file")
    End Try
    Return DataList
 End Function

 University Press & Assessment 2025                       Page 19 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

Python

def ReadData():
      DataList = []
      FileName = input("Enter the filename")
      try:
          File = open(FileName)
          for Line in File:
              DataList.append(Line)
          File.close()
      except:
          print("Cannot open file")
      return DataList

Mark points

  • Function header (and end)
  • Prompt to enter filename and reading input
  • Opening the file (to read) and closing the file in an appropriate place
  • Looping until EOF …
  • … reading each line in the file …
  • … (removing line break and) inserting in array
  • Returning populated array
  • Exception handling try catch with appropriate output

Part 2(b) — solution

  1. Procedure header (and end) taking (1D array) DataArray (of strings) as a parameter
  2. Declaration/use of 6 1D arrays (equivalent), one for each colour
  3. Looping through each line in parameter DataArray …
  4. … splitting by comma
  5. Comparing 2nd value/colour to each colour to select array …
  6. … storing 1st value/integer in correct array

Visual Basic (VB.NET)

Sub SplitData(DataArray())
    Dim Red(30) As String
    Dim Green(30) As String
    Dim Blue(30) As String
    Dim Orange(30) As String
    Dim Yellow(30) As String
    Dim Pink(30) As String

 University Press & Assessment 2025                       Page 22 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

      Dim RedNumber As Integer = 0
      Dim GreenNumber As Integer = 0
      Dim BlueNumber As Integer = 0
      Dim OrangeNumber As Integer = 0
      Dim YellowNumber As Integer = 0
      Dim PinkNumber As Integer = 0
      Dim x As Integer = 0
      Dim TempDataFromFile(1) As String
      Dim DataList(100, 1) As String

      For x = 0 To NumberItems - 1
         TempDataFromFile = (DataArray(x)).Split(",")
         DataList(x, 0) = TempDataFromFile(0)
         DataList(x, 1) = TempDataFromFile(1)
      Next x
      x = 0
      While DataList(x, 0) IsNot Nothing
         If DataList(x, 1) = "red" Then
             Red(RedNumber) = DataList(x, 0)
             RedNumber = RedNumber + 1
         ElseIf DataList(x, 1) = "green" Then
             Green(GreenNumber) = DataList(x, 0)
             GreenNumber = GreenNumber + 1
         ElseIf DataList(x, 1) = "blue" Then
             Blue(BlueNumber) = DataList(x, 0)
             BlueNumber = BlueNumber + 1
         ElseIf DataList(x, 1) = "orange" Then
             Orange(OrangeNumber) = DataList(x, 0)
             OrangeNumber = OrangeNumber + 1
         ElseIf DataList(x, 1) = "yellow" Then
             Yellow(YellowNumber) = DataList(x, 0)
             YellowNumber = YellowNumber + 1
         ElseIf DataList(x, 1) = "pink" Then
             Pink(PinkNumber) = DataList(x, 0)
             PinkNumber = PinkNumber + 1
         End If

 University Press & Assessment 2025                       Page 23 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

       x = x + 1
    End While
 End Sub

Python

def SplitData(DataArray):
      Red = []
      Green = []
      Blue = []
      Orange = []
      Yellow = []
      Pink = []

        for Line in DataArray:
            SplitLine = Line.split(",")

              if SplitLine[1].strip() == "red":
                  Red.append(SplitLine[0])
              elif SplitLine[1].strip() == "green":
                  Green.append(SplitLine[0])
              elif SplitLine[1].strip() == "blue":
                  Blue.append(SplitLine[0])
              elif SplitLine[1].strip() == "orange":
                  Orange.append(SplitLine[0])
              elif SplitLine[1].strip() == "yellow":
                  Yellow.append(SplitLine[0])
              else:
                  Pink.append(SplitLine[0])

Mark points

  • Procedure header (and end) taking (1D array) DataArray (of strings) as a parameter
  • Declaration/use of 6 1D arrays (equivalent), one for each colour
  • Looping through each line in parameter DataArray …
  • … splitting by comma
  • Comparing 2nd value/colour to each colour to select array …
  • … storing 1st value/integer in correct array

Part 2(c) — solution

  1. Procedure header taking (1D) array and filename as parameters, opening file to append and closing file (in appropriate
  2. Looping through each item in array parameter …
  3. … writing to the file
  4. … with new line break between each line
  5. Using exception handling try and catch with suitable output

Visual Basic (VB.NET)

Sub StoreData(DataToStore(), FileName)
    Dim FileWriter As IO.StreamWriter = New IO.StreamWriter(FileName, False)
    Dim x As Integer = 0
    Try
        While DataToStore(x) IsNot Nothing
           FileWriter.WriteLine(DataToStore(x))
           x = x + 1
        End While
        FileWriter.Close()
    Catch ex As Exception
        Console.WriteLine("Cannot open or write to file")
    End Try
 End Sub

 University Press & Assessment 2025                       Page 26 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

Python

def StoreData(DataToStore, FileName):
      try:
          File = open(FileName,"a+")
          for Item in DataToStore:
              File.write(Item)
              File.write("\n")
          File.close()
      except:
          print("Cannot create or write to file")

Mark points

  • Procedure header taking (1D) array and filename as parameters, opening file to append and closing file (in appropriate
  • Looping through each item in array parameter …
  • … writing to the file
  • … with new line break between each line
  • Using exception handling try and catch with suitable output

Part 2(d) — solution

  1. Calling StoreData with one array and filename
  2. Calling StoreData with remaining 5 arrays and filename

Visual Basic (VB.NET)

StoreData(Red, "Red.txt")
 StoreData(Green, "Green.txt")
 StoreData(Blue, "Blue.txt")
 StoreData(Orange, "Orange.txt")
 StoreData(Yellow, "Yellow.txt")
 StoreData(Pink, "Pink.txt")

Python

StoreData(Red, "Red.txt")
 StoreData(Green, "Green.txt")
 StoreData(Blue, "Blue.txt")
 StoreData(Orange, "Orange.txt")
 StoreData(Yellow, "Yellow.txt")
 StoreData(Pink, "Pink.txt")

Mark points

  • Calling StoreData with one array and filename
  • Calling StoreData with remaining 5 arrays and filename

Part 2(e)(i) — solution

  1. Calling ReadData() …
  2. … and storing/using return value
  3. Calling SplitData() with returned array as a parameter

Visual Basic (VB.NET)

Sub Main(args As String())
    Dim DataFromFile(,) As String = ReadData()
    SplitData(DataFromFile)
 End Sub

Python

DataFromFile = ReadData()
 SplitData(DataFromFile)

Mark points

  • Calling ReadData() …
  • … and storing/using return value
  • Calling SplitData() with returned array as a parameter

Part 2(e)(ii) — solution

  1. Prompt and input of filename TheData.txt
  2. Screenshot of data in red file. Filename must be shown in same screenshot as data

Mark points

  • Prompt and input of filename TheData.txt
  • Screenshot of data in red file. Filename must be shown in same screenshot as data

Question 3

Paper page 6 for Q3
Original paper page 6

Part 3(a)(i) — solution

  1. Class header (and end where appropriate)
  2. Constructor header (and end where appropriate) with (min) one parameter (integer) within class
  3. 3 attributes with correct data types
  4. NodeData has parameter assigned, LeftNode and RightNode are assigned null within constructor

Visual Basic (VB.NET)

Class Node
    Private NodeData As Integer
    Private LeftNode As Node
    Private RightNode As Node
    Sub New(pNodeData)
        NodeData = pNodeData
        LeftNode = Nothing
        RightNode = Nothing
    End Sub
 End Class

Python

class Node:
      def init (self, pNodeData):
         self. NodeData = pNodeData #integer
         self. LeftNode = None #node
         self. RightNode = None #node

Mark points

  • Class header (and end where appropriate)
  • Constructor header (and end where appropriate) with (min) one parameter (integer) within class
  • 3 attributes with correct data types
  • NodeData has parameter assigned, LeftNode and RightNode are assigned null within constructor

Part 3(a)(ii) — solution

  1. 1 get method with no parameter …
  2. … returning correct value
  3. 2nd and 3rd correct get methods

Visual Basic (VB.NET)

Function GetLeft()
    Return LeftNode
 End Function
 Function GetRight()
    Return RightNode
 End Function
 Function GetData()
    Return NodeData
 End Function

Python

def GetLeft(self):
      return self. LeftNode
 def GetRight(self):
      return self. RightNode
 def GetData(self):
      return self. NodeData

Mark points

  • 1 get method with no parameter …
  • … returning correct value
  • 2nd and 3rd correct get methods

Part 3(a)(iii) — solution

  1. 1 set method taking parameter of type Node …
  2. … assigning to correct attribute
  3. 2nd correct set method

Visual Basic (VB.NET)

Sub SetLeft(NewNode)
    LeftNode = NewNode
 End Sub
 Sub SetRight(NewNode)
    RightNode = NewNode
 End Sub

Python

def SetLeft(self, NewNode):
      self. LeftNode = NewNode
 def SetRight(self, NewNode):
      self. RightNode = NewNode

Mark points

  • 1 set method taking parameter of type Node …
  • … assigning to correct attribute
  • 2nd correct set method

Part 3(b) — solution

  1. Creating 1 instance of Node with a correct value and storing the node …
  2. … remaining 4 correct

Visual Basic (VB.NET)

Dim FirstNode As Node = New Node(10)
 Dim SecondNode As Node = New Node(20)
 Dim ThirdNode As Node = New Node(5)
 Dim FourthNode As Node = New Node(15)
 Dim FifthNode As Node = New Node(7)

Python

FirstNode = Node(10)
 SecondNode = Node(20)
 ThirdNode = Node(5)
 FourthNode = Node(15)
 FifthNode = Node(7)

Mark points

  • Creating 1 instance of Node with a correct value and storing the node …
  • … remaining 4 correct

Part 3(c)(i) — solution

  1. Class Tree header (and end) no inheritance and constructor header (and end) taking 1 node parameter within class …
  2. … storing parameter in FirstNode declared as a Node data type

Visual Basic (VB.NET)

Class Tree
    Private FirstNode As Node
    Sub New(pFirstNode)
        FirstNode = pFirstNode
    End Sub
 End Class

Python

class Tree:
      def init (self, FirstNode):
         self. FirstNode = FirstNode #node

Mark points

  • Class Tree header (and end) no inheritance and constructor header (and end) taking 1 node parameter within class …
  • … storing parameter in FirstNode declared as a Node data type

Part 3(c)(ii) — solution

  1. Get method header (and end) with no parameter, returning FirstNode

Visual Basic (VB.NET)

Function GetRootNode()
    Return FirstNode
 End Function

Python

def GetRootNode(self):
      return self. FirstNode

Mark points

  • Get method header (and end) with no parameter, returning FirstNode

Part 3(c)(iii) — solution

  1. Insert method header (and end) taking 1 node parameter
  2. If parameter < first node, checking if there is a left node …
  3. … storing node in left node if it is null
  4. If parameter >= first node, checking if there is a right node …
  5. … storing node in right node if it is null
  6. Looping until correct position is found // recursive calls

Visual Basic (VB.NET)

Function Insert(NewNode)
    Dim CurrentNode As Node
    CurrentNode = FirstNode
    Dim Inserted As Boolean = True
    While Inserted
        If NewNode.GetData() < CurrentNode.GetData() Then
           If CurrentNode.GetLeft() Is Nothing Then
              CurrentNode.SetLeft(NewNode)
              Return True
           Else
              CurrentNode = CurrentNode.GetLeft()
           End If

 University Press & Assessment 2025                       Page 38 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

       Else
           If CurrentNode.GetRight() Is Nothing Then
              CurrentNode.SetRight(NewNode)
              Return True
           Else
              CurrentNode = CurrentNode.GetRight()
           End If
       End If
    End While
 End Function

Python

def Insert(self, NewNode):
      CurrentNode = self. FirstNode
      Inserted = True
      while Inserted:
          if NewNode.GetData() < CurrentNode.GetData():
              if CurrentNode.GetLeft() == None:
                  CurrentNode.SetLeft(NewNode)
                  return True
              else:
                  CurrentNode = CurrentNode.GetLeft()
          else:
              if CurrentNode.GetRight() == None:
                  CurrentNode.SetRight(NewNode)
                  return True
              else:
                  CurrentNode = CurrentNode.GetRight()

Mark points

  • Insert method header (and end) taking 1 node parameter
  • If parameter < first node, checking if there is a left node …
  • … storing node in left node if it is null
  • If parameter >= first node, checking if there is a right node …
  • … storing node in right node if it is null
  • Looping until correct position is found // recursive calls

Part 3(d) — solution

  1. Procedure header (and end) taking node as parameter, that is recursive
  2. Checking if left is null and recursive call if not null
  3. Outputting node's data
  4. Checking if right is null and recursive call if not null
  5. Correct order

Visual Basic (VB.NET)

Sub OutputInOrder(RootNode)
    If RootNode.GetLeft() IsNot Nothing Then
        OutputInOrder(RootNode.GetLeft())
    End If
    Console.WriteLine(RootNode.GetData())
    If RootNode.GetRight() IsNot Nothing Then
        OutputInOrder(RootNode.GetRight())
    End If
 End Sub

Python

def OutputInOrder(RootNode):
      if RootNode.GetLeft() != None:
          OutputInOrder(RootNode.GetLeft())
      print(RootNode.GetData())
      if RootNode.GetRight() != None:
          OutputInOrder(RootNode.GetRight())

Mark points

  • Procedure header (and end) taking node as parameter, that is recursive
  • Checking if left is null and recursive call if not null
  • Outputting node's data
  • Checking if right is null and recursive call if not null
  • Correct order

Part 3(e)(i) — solution

  1. Creation of Tree object with the Node with value 10 as parameter
  2. Calling method Insert() for tree with the nodes for 20, 5, 15 and 7 in order
  3. Calling OutputInOrder() with tree's root node as parameter

Visual Basic (VB.NET)

Sub Main(args As String())
    Dim FirstNode As Node = New Node(10)
    Dim SecondNode As Node = New Node(20)
    Dim ThirdNode As Node = New Node(5)
    Dim FourthNode As Node = New Node(15)
    Dim FifthNode As Node = New Node(7)
    Dim MyTree As Tree = New Tree(FirstNode)
    MyTree.Insert(SecondNode)
    MyTree.Insert(ThirdNode)
    MyTree.Insert(FourthNode)
    MyTree.Insert(FifthNode)
    OutputInOrder(MyTree.GetRootNode())
 End Sub

 University Press & Assessment 2025                       Page 42 of 43

9618/41                                          Cambridge International AS & A Level – Mark Scheme   May/June 2025

Python

FirstNode = Node(10)
 SecondNode = Node(20)
 ThirdNode = Node(5)
 FourthNode = Node(15)
 FifthNode = Node(7)
 MyTree = Tree(FirstNode)
 MyTree.Insert(SecondNode)
 MyTree.Insert(ThirdNode)
 MyTree.Insert(FourthNode)
 MyTree.Insert(FifthNode)
 OutputInOrder(MyTree.GetRootNode())

Mark points

  • Creation of Tree object with the Node with value 10 as parameter
  • Calling method Insert() for tree with the nodes for 20, 5, 15 and 7 in order
  • Calling OutputInOrder() with tree's root node as parameter

Part 3(e)(ii) — solution

Output of 1

Mark points

  • Include the ideas shown in the solution above.

Question 20

Part 20 — solution

University Press & Assessment 2025 Page 43 of 43

Mark points

  • Include the ideas shown in the solution above.