Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (9618)
12 questions · Clear filters
Question text
9
(a) A stack has been implemented using pseudocode to store a maximum of 100 string items
using the global variables in the following table:
Initialisation
Identifier
Data type
Description
value
Base
INTEGER
0
pointer for the bottom of the stack
Top
INTEGER
‑1
pointer for the top of the stack
StackArray
STRING
[0:99]
1D array to implement the stack
Max
INTEGER
100
maximum number of items in the stack
The value of Top is incremented each time a data item is added to the stack and decremented
every time a data item is removed.
(i)
Complete the pseudocode for the function to remove a data item from the stack.
FUNCTION Pop() …
DECLARE DataItem : STRING
""
DataItem
IF … THEN
…
DataItem
…
Top
ELSE
"You cannot remove data; the stack is empty"
DataItem
ENDIF
…
ENDFUNCTION
[5]
(ii)
Write the pseudocode to output the data item removed from the stack with an appropriate
message.
…
… [1]
ĬÛú¾Ġ´íÈõÏĪÅĊÝùµĀ×
,
,
(b) A stack is used to implement recursion.
State the three essential features of recursion.
1 …
…
2 …
…
3 …
…
[3]
Question text
12 (a) A stack has been implemented using pseudocode to store a maximum of 100 string items
using the global variables in the following table:
Initialisation
Identifier
Data type
Description
value
Base
INTEGER
0
pointer for the bottom of the stack
Top
INTEGER
-1
pointer for the top of the stack
StackArray
STRING
[0:99]
1D array to implement the stack
Max
INTEGER
100
maximum number of items in the stack
The value of Top is incremented each time a data item is added to the stack and decremented
each time a data item is removed. If the stack is full, an appropriate error message is output.
(i)
Complete the pseudocode for the procedure to add a data item onto the stack.
PROCEDURE Push( … )
IF Top < Max – 1 THEN
Top ← …
… ← NewData
ELSE
OUTPUT …
ENDIF
ENDPROCEDURE
[4]
(ii)
Write pseudocode to input a new data item and add it to the stack using Push().
…
…
…
… [2]
(b) Explain the reasons why a stack is used when a recursive algorithm is executed.
…
…
…
…
…
… [3]
ĬÑĊ®Ġ´íÈõÏĪÅĊàû·Ā×
,
,
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
ĬÍĊ®Ġ´íÈõÏĪÅĊÝú¶Ą×
Question text
10 A stack, StackArray, is to be implemented using pseudocode, to store a maximum of 100 string
items in an appropriate array. Declarations are required so that the stack has a beginning, an end
and a maximum size. An array is also required to store the data.
(a) Write pseudocode to declare the variables, constant and array required to implement the
stack.
…
…
…
…
…
… [3]
(b) Write pseudocode for a procedure to initialise the top and bottom pointers of the stack to
appropriate values.
…
…
…
…
…
… [3]
ĬÙú¾Ġ´íÈõÏĪÅĊàù¶Ā×
Question text
11
Describe when the use of recursion would be beneficial and give an example.
Description …
…
…
…
Example …
…
[3]
Question text
11
The following diagram shows an ordered binary tree.
Red
Green
Yellow
Blue
Orange
Violet
Indigo
(a) 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.
RootPtr
LeftPtr
Data
RightPtr
Red
Green
–1
Blue
–1
[4]
ĬÑĊ®Ġ´íÈõÏĪÅĊàú·Ā×
,
,
(b) 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).
RootPtr
Index
LeftPtr
Data
RightPtr
0
Red
0
Green
1
Yellow
2
Blue
3
Orange
4
Indigo
5
Violet
FreePtr
6
7
[4]
(c) 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
[4]
ĬÓĊ®Ġ´íÈõÏĪÅĊàü·Ā×
,
,
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
ĬÍĊ®Ġ´íÈõÏĪÅĊÝû¶Ą×
Question text
11
This binary tree shows an ordered list of integers.
25
4
36
1
16
64
9
49
(a) 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.
RootPtr
LeftPtr
Data RightPtr
25
4
–1
1
–1
[4]
ĬÑĊ®Ġ´íÈõÏĪÅĊàù¸Ā×
,
,
(b) 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).
RootPtr
Index
LeftPtr
Data
RightPtr
0
25
0
4
1
36
2
1
3
16
4
64
5
9
FreePtr
6
49
7
8
[4]
(c) 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) …
−1
NullPtr
RootPtr
…
WHILE NowPtr <> NullPtr
IF LinkList[NowPtr].Data < Item THEN
NowPtr
LinkList[NowPtr].RightPtr
ELSE
IF … THEN
NowPtr
…
ELSE
RETURN NowPtr
ENDIF
ENDIF
ENDWHILE
RETURN NullPtr
ENDFUNCTION
[4]
ĬÓĊ®Ġ´íÈõÏĪÅĊàû¸Ā×
,
,
Permission to reproduce items where third‑party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer‑related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
ĬÍĊ®Ġ´íÈõÏĪÅĊÝüµĄ×
Question text
11
The following diagram shows an ordered binary tree.
Red
Green
Yellow
Blue
Orange
Violet
Indigo
(a) 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.
RootPtr
LeftPtr
Data
RightPtr
Red
Green
–1
Blue
–1
[4]
ĬÑĊ®Ġ´íÈõÏĪÅĊßû·Ā×
,
,
(b) 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).
RootPtr
Index
LeftPtr
Data
RightPtr
0
Red
0
Green
1
Yellow
2
Blue
3
Orange
4
Indigo
5
Violet
FreePtr
6
7
[4]
(c) 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
[4]
ĬÓĊ®Ġ´íÈõÏĪÅĊßù·Ā×
,
,
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
ĬÍĊ®Ġ´íÈõÏĪÅĊÞú¶Ą×
Question text
10 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.
(a) Write the pseudocode for the required declarations.
…
…
…
…
…
… [3]
(b) Complete the pseudocode function Pop() to pop an item from Stack.
// popping an item from the stack
FUNCTION Pop() …
DECLARE Item : REAL
0
Item
… BasePointer THEN
…
Item
…
TopPointer
ELSE
OUTPUT "The stack is empty – error"
ENDIF
…
ENDFUNCTION
[5]
(c) Compare and contrast the queue and stack Abstract Data Types (ADT).
…
…
…
…
…
… [2]
Question text
9
(a) A stack Abstract Data Type (ADT) is to be implemented using pseudocode, with procedures
to initialise it and to push new items onto the stack.
A 1D array Stack stores the contents of the stack.
(i)
Study the pseudocode in part (a)(ii) and complete the table of identifiers by writing the
missing data types and descriptions.
Identifier
Data type
Description
BasePointer
TopPointer
Stack
REAL
[2]
(ii)
Complete the pseudocode.
CONSTANT MaxSize = 40
DECLARE BasePointer : INTEGER
DECLARE TopPointer : INTEGER
DECLARE Stack : ARRAY[1:40] OF REAL
// initialisation of stack
PROCEDURE Initialise()
…
1
…
0
ENDPROCEDURE
// push an item onto the stack
PROCEDURE Push(NewItem : REAL)
… MaxSize THEN
…
…
Stack[TopPointer]
ENDIF
ENDPROCEDURE
[5]
(b) Justify the use of a linked list instead of an array to implement a stack.
…
…
…
… [2]
(c) Explain how a compiler makes use of a stack when translating recursive programming code.
…
…
…
…
…
…
…
… [4]
Question text
12 (a) The array Names[0:99] is in alphabetical order.
Complete this pseudocode binary search algorithm.
← 0
Lower
…
←
Mid
0
←
Exit
FALSE
OUTPUT "Enter the name to be found "
INPUT Target
REPEAT
… THEN
OUTPUT Target, " does not exist"
←
Exit
TRUE
ENDIF
←
Mid
Lower + (Upper – Lower + 1) DIV 2
IF Names[Mid] < Target THEN
←
…
Lower
ENDIF
IF Names[Mid] > Target THEN
…
ENDIF
… THEN
OUTPUT Target, " was found at location ", Mid
←
Exit
TRUE
ENDIF
…
[6]
(b) Big O notation is used to classify efficiency of algorithms.
The Big O notation for time complexity in a binary search is O(log n).
(i)
State the Big O notation for time complexity of a linear search.
… [1]
(ii)
Describe the meaning of O(log n) as it applies to a binary search algorithm.
…
…
…
… [2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
11
A simplified linked list is used to store the names of flowers in alphabetical order. It is implemented
using two 1D arrays:
•
Flower stores the names of the flowers.
•
NextPointer stores the pointer to the next flower name in the list.
HeadPointer indicates the index of the first flower name in the linked list.
HeadPointer
6
When the end of the linked list is reached, the next pointer has the value of 0.
The following table shows the initial content of the arrays.
Index
Flower
NextPointer
1
Rose
7
2
Marigold
1
3
Foxglove
10
4
Iris
9
5
Daisy
3
6
Dahlia
5
7
Saxifrage
0
8
Lupin
2
9
Lily
8
10
Hydrangea
4
(a) Several flower names have been deleted from the linked list. These are crossed out in the
following table.
Complete the table to show the new values of HeadPointer and NextPointer to keep the
remaining flower names in alphabetical order.
HeadPointer
Index
Flower
NextPointer
1
Rose
2
Marigold
3
Foxglove
4
Iris
5
Daisy
6
Dahlia
7
Saxifrage
8
Lupin
9
Lily
10
Hydrangea
[3]
(b) Complete the pseudocode algorithm so that it achieves the following when applied to the
arrays:
•
The flower name is input.
•
The linked list is searched, in order, for the flower name.
•
If the flower name is found, an appropriate message is output to indicate it has been
found.
•
If the flower name is not found, an appropriate message is output to indicate it has not
been found.
•
The algorithm terminates when the next pointer value is 0.
HeadPointer
Pointer
0
Found
OUTPUT "Enter a flower name "
… .
… .
IF Flower[Pointer] = FlowerName THEN
Pointer
Found
0
Pointer
ELSE
… .
ENDIF
ENDWHILE
… .
OUTPUT Flower[Found], " is found"
ELSE
… .
ENDIF
[5]
(c) Explain how you could improve the simplified linked list structure.
…
…
…
… [2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
10 (a) State three essential features of recursion.
1 …
…
2 …
…
3 …
…
[3]
(b) Explain the reasons why a stack is a suitable Abstract Data Type (ADT) to implement
recursion.
…
…
…
…
…
… [3]
(c) Identify two ADTs other than a stack.
1 …
2 …
[2]
(d) The function StackFull() checks whether a stack is full.
The function uses the variable TopOfStack to represent the pointer to the most recent
position used on the stack, and the variable Max to represent the maximum size of the stack.
Assume TopOfStack and Max are global variables.
FUNCTION StackFull() RETURNS BOOLEAN
IF TopOfStack = Max THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
An algorithm AddInteger is required to add a new integer data element to a stack.
The stack is implemented as an array ArrayStack.
The function AddInteger() calls StackFull() and returns an appropriate message.
Complete the pseudocode for the function AddInteger().
FUNCTION AddInteger(NewInteger : INTEGER) RETURNS STRING
…
…
…
…
…
…
…