Practice Paper Set 2 Algorithms Programming
Practice Paper Set 2 Algorithms Programming
Practice Paper Set 2 Algorithms Programming
Do not use:
• a calculator
First name
Last name
Centre Candidate
number number
INSTRUCTIONS
• Use black ink.
• Complete the boxes above with your name, centre number and candidate number.
• Answer all the questions.
• Write your answer to each question in the space provided. Additional paper may be
used if required but you must clearly show your candidate number, centre number and
question number(s).
• Do not write in the barcodes.
INFORMATION
• The total mark for this paper is 140.
• The marks for each question are shown in brackets [ ].
• Quality of extended responses will be assessed in questions marked with an
asterisk (*).
• This document consists of 28 pages.
Section A
1 A binary search tree, colour, stores data about colours that are entered into a computer.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [3]
(ii) State the features that make a tree a binary search tree.
...........................................................................................................................................
...................................................................................................................................... [1]
Green
Black Magenta
Blue Indigo
Add the following colours to the tree above in the order written:
50
45 76
20 48 60 98
5 31 88
92
(i) Explain, using the binary search tree numbers as an example, how a depth-first
(post-order) traversal is performed.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [5]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [5]
68 30 73 22 1 90 70
The following table shows the data stored in the array. The Root Pointer stores the node
number of the first element in the tree.
Right
Root Pointer Array Index Left Pointer Data
Pointer
0 1 68 2
0 1 30
2 73
3 22
Free Pointer 4 1
5 90
7 6 70
Table 1.1
(i) Complete the remaining Left Pointer and Right Pointer values for the data entered in
Table 1.1. Where the pointer is null, leave the space empty. [3]
...........................................................................................................................................
...................................................................................................................................... [1]
(iii) The following data is added to the array in the given order:
6 100
Add the new nodes to Table 1.1 and update any relevant pointers. [4]
51
25 G L
H 307
70 50
80 176 E
185
M 0
210
233 20
90
Fig. 2.1
(a) State the full name of the data structure shown in Fig. 2.1.
.............................................................................................................................................. [2]
(b) The structure in Fig. 2.1 is searched using the A* algorithm making use of the heuristic values.
(i) State what the heuristic values could represent in Fig. 2.1.
...........................................................................................................................................
...................................................................................................................................... [1]
...........................................................................................................................................
...................................................................................................................................... [1]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [8]
Decision .............................................................................................................................
...........................................................................................................................................
Effect ..................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [3]
Explain how concurrent processing could be used in searching algorithms, and evaluate the
benefits and trade-offs from implementing concurrent processing in a searching algorithm.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [9]
© OCR 2016 H446/02
11
3 Dexter is leading a programming team who are creating a computer program that will simulate an
accident and emergency room to train hospital staff.
(a) Identify two features of the problem that make it solvable by computational methods.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [2]
(b)* Dexter has used decomposition and abstraction during the analysis of the problem.
Explain and evaluate the use of decomposition and abstraction in the creation of this
simulation.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [9]
© OCR 2016 H446/02 Turn over
12
(c) Dexter has been told he should make use of caching in the simulation.
Describe what is meant by caching and explain how caching can be used within the simulation.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [4]
(d) Two of Dexter’s programmers have developed different solutions to one part of the problem.
Table 3.1 shows the Big O time complexity for each solution, where n = the number of data
items.
Solution A Solution B
O(kn)
Space O(log n)
(where k > 1)
Table 3.1
(i) The Big O time complexity for time of each solution is O(n).
Explain what is meant by time complexity, with reference to the solutions’ Big O time
complexity.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [3]
Solution A ...........................................................................................................................
Solution B ...........................................................................................................................
[2]
(iii) Explain, with reference to the Big O complexities of each solution, which solution you
would suggest Dexter chooses.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [4]
Describe how the programmers could make use of the following IDE tools:
Breakpoints ................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Stepping ....................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
[4]
Fig. 4.1
(a) Show how a bubble sort would sort the data in Fig. 4.1.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [6]
Annotate your pseudocode with comments to show how it solves the problem.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [7]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [4]
(ii) Name two sorting algorithms, other than a bubble sort and merge sort.
1 .........................................................................................................................................
2 .........................................................................................................................................
[2]
(d) Show how a binary search would be performed on the array shown in Fig. 4.2 to find the
value ‘duck’.
Fig. 4.2
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [3]
5 Kim is writing an object-oriented program for a four player board game. The board has 26 squares
that players move around, as shown in Fig. 5.1.
Start 1 2 3 4 5 6 7
25 8
24 9
23 10
Deck
22 11
21 12
Miss a
20 19 18 17 16 15 14
turn
Fig. 5.1
Each player takes it in turn to roll two dice. They then move that number of spaces on the board.
If they roll a double (both dice have the same value), they then take a card from the deck. The
deck contains 40 cards that each include a sentence (such as “You have won the lottery”). The
sentence on the card determines if money is given or taken away from the player.
Fig. 5.3 shows a class diagram for Player. A class diagram describes a class. It contains
the class name, followed by the attributes, then the methods.
Player
playerID: STRING
boardPosition: INTEGER
money: INTEGER
constructor()
getPosition()
setPosition(position)
getMoney()
setMoney(amount)
Fig. 5.3
The constructor creates a new instance of Player, taking the player’s ID as a parameter.
The board position is set to 0, and money to £2000.
Write, using pseudocode, the constructor method for the Player class.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [3]
Animal
name: STRING
currentLevel: INTEGER
cost: INTEGER
L0: REAL
L1: REAL
L2: REAL
L3: REAL
imageLink: STRING
setSquare: INTEGER
owned: STRING
constructor()
getCost()
upgrade(player)
getCurrentLevel()
setOwned(player)
getOwned()
getAmountToCharge()
getName()
Fig. 5.4
The constructor takes the required data as parameters and then sets currentLevel
to 0, and assigns the parameters as the remaining attributes for the new object.
Write, using pseudocode, the constructor method for the Animal class. [4]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
(iii) Write, using pseudocode, the code to create an instance of Animal for the Squirrel
shown in Fig. 5.2, positioned on square number 6, for the constructor function you wrote
in part (a)(ii).
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [2]
function playerMove(currentPlayer)
dice1 = random(1,6)
dice2 = random(1,6)
pickDeck(currentPlayer)
endif
currentPlayer.setMoney(currentPlayer.getMoney() + .............)
endif
missAGo(currentPlayer)
checkAnimal(currentPlayer)
endif
.................................................................
endfunction
[6]
Explain the difference, benefits and drawbacks between passing by value and by reference.
Recommend which should be used for currentPlayer, justifying your decision.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...................................................................................................................................... [9]
© OCR 2016 H446/02
23
(c) The deck is stored as a zero-indexed 1D array, named deck, of type Card.
Card
textToDisplay: STRING
amount: INTEGER
constructor()
getTextToDisplay()
getAmount()
Fig. 5.5
The array, deck, is treated as a queue, with a variable, headPointer, identifying the first
card in the deck. When a card has been used, the head pointer increases to move to the next
position. If the end of the deck is reached, the head pointer returns to 0 and starts again.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [6]
© OCR 2016 H446/02 Turn over
24
(d) The procedure checkAnimal:
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
© OCR 2016 H446/02
25
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................