Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q3 · 9618/31/O/N/21 Q10

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

Q39618/31/O/N/21original Q10Structured · 13 marks
Question 10 from 9618/31/O/N/21

Worked solution

(a)

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.

(b)

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.

(c)

Write any two of these three:

Queue [1]

Linked list [1]

Other points that also score: Binary tree.

(d)

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
ENDFUNCTION

Any 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].

KEY IDEARecursion needs a base case and a self-calling general case, and the stack stores each call so it can unwind in reverse order.
TOTAL 13 marks

Official mark scheme

Mark scheme for 9618/31/O/N/21 question 10
VIDEO
▶ 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.