Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q23 · 9618/32/O/N/25 Q12

Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.

Q239618/32/O/N/25original Q12Structured · 9 marks
Question 12 from 9618/32/O/N/25

Worked solution

(a)(i)

Fill in the four gaps. The parameter is the new item, a string. Top goes up by 1 before the item is stored. If the stack is full, output a message.

PROCEDURE Push(NewData : STRING)
    IF Top < Max - 1 THEN
        Top ← Top + 1
        StackArray[Top] ← NewData
    ELSE
        OUTPUT "Stack full; new data cannot be added"
    ENDIF
ENDPROCEDURE

Parameter written as NewData : STRING [1]

Top ← Top + 1 [1]

StackArray[Top] ← NewData [1]

OUTPUT of a suitable error message such as "Stack full" [1]

(a)(ii)

Input the item into a variable, then call Push with that same variable.

INPUT MyData
CALL Push(MyData)

INPUT into a variable (a prompt is optional) [1]

CALL Push with the input variable as the parameter [1]

(b)

A stack is Last In First Out (LIFO), so the last item stored is the first one taken off. [1]

Each time the recursive algorithm calls itself, data (such as the return address and parameters) is pushed onto the stack. [1]

When the base case is reached, the calls start to unwind and the data is popped in the reverse order to which it was pushed. [1]

KEY IDEAA stack holds the data of each recursive call and gives it back in reverse order as the calls unwind.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/32/O/N/25 question 12
VIDEO
▶ Whole paper

Video solution of the whole paper · by Zafar Ali Khan (ZAK). 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.