Algorithms (Cambridge (CIE) A Level Computer Science): Exam Questions

Exam code: 9618

58 mins7 questions
1a4 marks

The following diagram shows an ordered binary tree.

Hierarchy diagram with "Red" at the top, branching to "Green" and "Yellow", with further branches to "Blue", "Orange", "Violet", and "Indigo".

A linked list of nodes is used to store the data. Each node consists of a left pointer, the data and a right pointer.

–1 is used to represent a null pointer.

Complete this linked list to represent the given binary tree.

Diagram of a binary tree with nodes labelled 'RootPtr', 'Red', 'Green', and 'Blue'. Each node has 'LeftPtr', 'Data', and 'RightPtr' fields.
1b4 marks

A user-defined record structure is used to store the nodes of the linked list in part (a).

Complete the diagram, using your answer for part (a).

Binary tree table with columns: Index, LeftPtr, Data, RightPtr. Colours listed: Red, Green, Yellow, Blue, Orange, Indigo, Violet. RootPtr is 0.
1c1 mark

The linked list in part (a) is implemented using a 1D array of records. Each record contains a left pointer, data and a right pointer.

The following pseudocode represents a function that searches for an element in the array of records BinTree. It returns the index of the record if the element is found, or it returns a null pointer if the element is not found.

Complete the pseudocode for the function.

FUNCTION SearchTree(Item : STRING) ........................................................................

NowPtr .........................................................................................................................

WHILE NowPtr <> -1

IF ..................................................................................................................... THEN

NowPtr BinTree[NowPtr].LeftPtr

ELSE

IF BinTree[NowPtr].Data < Item THEN

.................................................................................................................

ELSE

RETURN NowPtr

ENDIF

ENDIF

ENDWHILE

RETURN NowPtr

ENDFUNCTION

2a4 marks

This binary tree shows an ordered list of integers.

Binary tree diagram with root node 25, branching to 4 and 36. Nodes 4 connects to 1 and 16, 16 to 9. Node 36 connects to 64, 64 to 49.

A linked list of nodes is used to store the data. Each node consists of a left pointer, the data and a right pointer.

−1 is used to represent a null pointer.

Complete this linked list to represent the given binary tree organisation.

Diagram of a binary tree with nodes labelled RootPtr, LeftPtr, Data, RightPtr, with values 25 and 4, pointing to arrays with values -1, 1, -1.
2b4 marks

A 2D array is used to store the nodes of the linked list in part (a). Complete the diagram using your answer for part (a)

Binary tree data structure table with columns Index, LeftPtr, Data, RightPtr; rows filled, RootPtr set to 0, and FreePtr left blank.
2c4 marks

The linked list in part (a) is implemented using a 1D array of records. Each record contains a left pointer, data and a right pointer.

The following pseudocode represents a function that searches for an element in the array of records LinkList. It returns the index of the record if the element is found, or it returns a null pointer if the element is not found.

Complete the pseudocode for the function.

FUNCTION SearchList(Item : INTEGER) ........................................................................

NullPtr leftwards arrow −1

.................................................................... RootPtr

WHILE NowPtr <> NullPtr

IF LinkList[NowPtr].Data < Item THEN

NowPtr leftwards arrow LinkList[NowPtr].RightPtr

ELSE

IF ..................................................................... THEN

NowPtr leftwards arrow..............................................................................

ELSE

RETURN NowPtr

ENDIF

ENDIF

ENDWHILE

RETURN NullPtr

ENDFUNCTION

3a1 mark

State a condition that must be true for an array to be searchable for a binary search.

3b5 marks

Complete the given pseudocode to find an item in a 1D array Names of type STRING using a binary search.

DECLARE Names : ARRAY[1:100000] OF STRING

DECLARE TopOfList : INTEGER

DECLARE EndOfList : INTEGER

DECLARE CurrentItem : INTEGER

DECLARE ToFind : STRING

DECLARE Found : BOOLEAN

DECLARE NotInList : BOOLEAN

TopOfList ← 1

EndOfList ← 100000

OUTPUT "Which name do you wish to find? "

INPUT ToFind

...................................................................................................................................................

NotInList ← FALSE

WHILE ................................................ AND ................................................

CurrentItem ← (TopOfList + EndOfList) DIV 2

IF ........................................................................................................... THEN

Found ← TRUE

ELSE

IF TopOfList >= EndOfList THEN

...........................................................................................................

ELSE

IF ToFind > Names[CurrentItem] THEN

...........................................................................................................

ELSE

EndOfList ← CurrentItem – 1

ENDIF

ENDIF

ENDIF

ENDWHILE

IF Found = TRUE THEN

OUTPUT "Item found at position ", CurrentItem, " in array"

ELSE

OUTPUT "Item not in array"

ENDIF

3c2 marks

Describe the performance of a binary search in relation to the number of data items in the array being searched. Refer to Big O notation in your answer.

4a4 marks

Complete the pseudocode to find an item in a 1D array Widgets of type STRING, using a linear search.

DECLARE Widgets : ARRAY[1:50000] OF STRING

DECLARE TopOfList : INTEGER

DECLARE EndOfList : INTEGER

DECLARE Count : INTEGER

DECLARE ToFind : STRING

DECLARE Found : BOOLEAN

DECLARE NotInList : BOOLEAN

TopOfList ← 1

EndOfList ← 50000

OUTPUT "Enter the name of the item you wish to find "

INPUT ToFind

Found ← FALSE

NotInList ← FALSE

Count ← TopOfList

WHILE Found = FALSE AND NotInList = FALSE

IF Widgets[Count] = ToFind THEN

Found ← TRUE

ENDIF

Count ← Count + 1

IF Count > EndOfList THEN

NotInList ← TRUE

ENDIF

ENDWHILE

IF Found = TRUE THEN

OUTPUT "Item found at position ", Count - 1, " in array"

ELSE

OUTPUT "Item not in array"

ENDIF

4b4 marks

Compare the methods used by the linear and binary search algorithms to find an item in an array. Refer to Big O notation in your answer.

5a4 marks

Show how the contents of the following stack will change as the RPN expression in part (a)(i) is evaluated.

Nine vertical columns, each containing five equally-sized rectangles arranged in a single file line.
5b3 marks

Explain how a stack can be used to evaluate RPN expressions.

6a3 marks

A stack is to be set up using the information in the table.

Identifier

Data type

Description

BasePointer

INTEGER

points to the bottom of the stack

TopPointer

INTEGER

points to the top of the stack

Stack

REAL

1D array to implement the stack

A constant, with identifier Capacity, limits the size of the stack to 25 items.

Write the pseudocode for the required declarations.

6b5 marks

Complete the pseudocode function Pop() to pop an item from Stack.

// popping an item from the stack

FUNCTION Pop()...............................................................................

DECLARE Item : REAL

Itemleftwards arrow0 ........................................BasePointer THEN

Itemleftwards arrow ........................................

TopPointer ..................................................

ELSE

OUTPUT "The stack is empty – error"

ENDIF

.........................................................................

ENDFUNCTION

6c2 marks

Compare and contrast the queue and stack Abstract Data Types (ADT).

74 marks

The Reverse Polish Notation (RPN) expression:

a b * 2 / c d / *

is to be evaluated where a = 20, b = 3, c = 10 and d = 5.

Show the changing contents of the following stack as the RPN expression is evaluated.

Eleven columns, each with five vertically aligned empty rectangles, arranged side by side on a white background.