Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.
Write any three of these:
There must be a base case (stopping condition) [1]
There must be a general case [1]
...which calls itself (the function is defined in terms of itself) [1]
Other points that also score: each call changes the state so it moves towards the base case; unwinding can happen once the base case is reached.
Write any three of these:
A stack is a LIFO (last in, first out) structure [1]
Each recursive call is pushed onto the stack [1]
...and is popped off when the function ends [1]
Other points that also score: this allows backtracking / unwinding to keep the right order.
Write any two of these three:
Queue [1]
Linked list [1]
Other points that also score: Binary tree.
Call StackFull(). If the stack is full, return a message. Otherwise add the integer and return a message.
FUNCTION AddInteger(NewInteger : INTEGER) RETURNS STRING
IF StackFull() THEN
RETURN "The stack is full"
ELSE
TopOfStack ← TopOfStack + 1
ArrayStack[TopOfStack] ← NewInteger
RETURN "Item added"
ENDIF
ENDFUNCTIONAny five of these seven points score (any wording is fine for the messages):
Test the stack with IF ... THEN ... ELSE ... ENDIF [1]
Use StackFull() correctly in the test [1]
Return a suitable message when the stack is full [1]
Return a message when there is space [1]
Add 1 to TopOfStack when there is space [1]
Other points that also score: use NewInteger as the data stored; store it in the correct element, ArrayStack[TopOfStack].
▶ Whole paper
Video solution of the whole paper · by qiphyedu. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.