Paper 1 Implementing Algorithms Answers
These answers correspond to Paper 1 Implementing Algorithms Drills.
Answer 1: Implementation Preconditions
- Binary search requires the array/list to be sorted and indexable.
- BST search requires the tree to obey the BST ordering rule used by the implementation, e.g. smaller values go left and larger values go right; if duplicates are allowed, the duplicate policy must be consistent.
Mark points:
- 1 mark for sorted indexed data for binary search;
- 1 mark for BST ordering invariant.
Common weak answer:
- saying binary search only needs “many values”; the important precondition is sorted order.
Answer 2: Array Stack Trace
Trace:
| Operation | array state | top |
|---|---|---|
| start | [-, -, -, -] | -1 |
PUSH 11 | [11, -, -, -] | 0 |
PUSH 6 | [11, 6, -, -] | 1 |
POP | [11, -, -, -] | 0 |
PUSH 9 | [11, 9, -, -] | 1 |
PUSH 14 | [11, 9, 14, -] | 2 |
Final state:
items = [11, 9, 14, -]
top = 2Mark points:
- push
11at index0; - push
6then pop it; - push
9at index1; - push
14at index2; - final
topis2.
Common weak answer:
- leaving
6in the stack afterPOP.
Answer 3: Circular Queue Full and Empty Checks
- Empty condition:
count = 0. - Full condition:
count = capacity, so herecount = 5. - Next rear index:
(1 + 1) MOD 5 = 2. - Modulo arithmetic wraps the rear or front index back to
0after the end of the array is reached.
This uses the convention that rear stores the index of the most recently enqueued item, so the next insertion position is (rear + 1) MOD capacity.
Mark points:
- correct empty condition;
- correct full condition;
- correct next rear index
2; - explains wrap-around.
Common weak answer:
- checking only whether
rearis at the final array index. A circular queue may wrap and use earlier positions.
Answer 4: Linked Insert at Head
Correct creation/use and pointer updates:
newNode.data <- 12
newNode.next <- head
head <- newNodeThe essential pointer order is:
newNode.next <- head
head <- newNodeFinal list:
head -> [12 | next] -> [20 | next] -> [35 | NULL]Mark points:
- create/use the new node containing
12; - point the new node to the old head first;
- update
headto point to the new node.
Common weak answer:
- updating
headfirst, which loses access to the original list.
Answer 5: BST Insert Trace
Comparisons:
25 < 45, so move left to 20.
25 > 20, so move right to 30.
25 < 30, so insert as the left child of 30.Updated tree:
45
/ \
20 70
/ \ /
10 30 60
/
25Mark points:
- starts comparison at root
45; - moves left to
20; - moves right to
30; - places
25as left child of30; - preserves the rest of the tree.
Common weak answer:
- inserting
25as the right child of20directly even though30already occupies that position.
Answer 6: Serial File Search
Records inspected:
C03,Asha
C01,Ben
C04,Chen
C02,DivyaThe search stops when C02,Divya is found. Because the file is serial, there is no useful key order, so the program cannot stop early before either finding the target or reaching the end of the file.
Mark points:
- records are read from the start;
- all four records are inspected before
C02is found; - search stops when the matching key is found.
Common weak answer:
- assuming the records are sorted by key and stopping at
C03.
Answer 7: Sequential File Search
Records inspected:
101,Asha
105,Ben
109,ChenThe target key is 107. When the program reaches key 109, the current key has passed 107. Since the file is ordered, 107 cannot appear later, so the search can stop and return not found.
Mark points:
- reads
101; - reads
105; - reads
109; - explains early stop because
109 > 107in an ordered file.
Common weak answer:
- continuing to inspect
114,Divya; this is unnecessary once the key has been passed.
Answer 8: Implementation Choice
- Sorted Python list of 1000 IDs: binary search, because the list is sorted and indexable.
- Browser Back button: stack, because the most recent page should be returned to first.
- Print jobs in arrival order: queue, because jobs should be processed first-in-first-out.
Mark points:
- binary search linked to sorted indexed data;
- stack linked to last-in-first-out;
- queue linked to first-in-first-out.
Common weak answer:
- choosing linear search for the sorted list without mentioning why binary search is available.
Answer 9: Pseudocode Completion
Completed pseudocode:
FUNCTION Pop()
IF top = -1 THEN
RETURN "UNDERFLOW"
ENDIF
item <- stack[top]
top <- top - 1
RETURN item
ENDFUNCTIONitem is saved first because after top is decremented, stack[top] would refer to a different position.
Mark points:
- 1 mark for
item <- stack[top]; - 1 mark for
top <- top - 1; - 1 mark for
RETURN item; - 1 mark for explaining why the item is saved before changing
top.
Common weak answer:
- decrementing
topbefore savingstack[top], which returns the wrong item.
Answer 10: Error Identification
The error is that head <- newNode is done before newNode.next <- head. After head is changed, head no longer refers to the old first node, so the original list is lost.
Correct order:
newNode.data <- 12
newNode.next <- head
head <- newNodeMark points:
- identifies that updating
headtoo early loses the old list; - explains that
newNode.nextpoints to the new node itself in the faulty code; - gives
newNode.next <- headbefore updatinghead; - gives
head <- newNodeafter preserving the old head link.
Common weak answer:
- only swapping the first two lines. The data assignment is not the problem; the pointer order is.