Full Marks

A-level Computer Science Year 13 · Topic 8

Algorithms and problem solving

Algorithms and problem solving is about the strategies programmers use to crack hard problems.

We will use recursion, divide and conquer, backtracking and heuristics.

Then we apply computational methods like data mining and performance modelling, abstraction and automation, and a systematic approach from analysis to evaluation.

In this topic

  1. 8.1Recursion and recursive algorithmsFree below · 24 questions
  2. 8.2Divide and conquer, backtracking and heuristicsOpen in Full Marks · 25 questions
  3. 8.3Computational methods: problem recognition, decomposition, data mining, performance modelling and visualisationOpen in Full Marks · 25 questions
  4. 8.4Abstraction, automation and computational thinkingOpen in Full Marks · 29 questions
  5. 8.5Systematic approach to problem solving: analysis, design, implementation, testing and evaluationOpen in Full Marks · 25 questions
  6. 8.6Artificial intelligence: graphs, Dijkstra's and A* algorithms, and machine learningOpen in Full Marks · 23 questions

8.1 · Free notes

Recursion and recursive algorithms

Diagram: Recursion and recursive algorithms

Key points

In more detail

Practice

Recursion and recursive algorithms: 10 questions

Pick an answer to check it, or open the answer for the explanation.

1. Which feature of a recursive subroutine stops it calling itself indefinitely?

  1. The return address
  2. The base case
  3. The general case
  4. The stack pointer
Show answer

B: The base case. The base case returns a value without making another recursive call. The general case makes the call, and each call must move closer to the base case.

2. fact(n) returns 1 when n = 0, otherwise n × fact(n − 1). How many calls to fact are made in total, including the first, when fact(5) is evaluated?

  1. 6
  2. 7
  3. 4
  4. 5
Show answer

A: 6. The calls are fact(5), fact(4), fact(3), fact(2), fact(1) and fact(0), which is 6. The call with n = 0 is the base case.

3. Where does a computer store the parameters, local variables and return address for each recursive call?

  1. On the call stack as a stack frame
  2. In a hash table
  3. In the processor's ALU
  4. In a queue in main memory
Show answer

A: On the call stack as a stack frame. Each call pushes a stack frame onto the call stack. When the call returns, its frame is popped (LIFO) and execution resumes at the stored return address.

4. A recursive function has no reachable base case. What is the most likely result when it runs?

  1. A stack overflow
  2. It returns 0
  3. It runs once and stops
  4. A syntax error before running
Show answer

A: A stack overflow. Calls keep being pushed onto the call stack with none returning, until the stack memory is exhausted and a stack overflow occurs.

5. def f(n): if n == 0: return 0 else: return n + f(n - 1). What value does f(4) return?

  1. 6
  2. 4
  3. 10
  4. 24
Show answer

C: 10. f(4) = 4 + f(3) = 4 + 3 + f(2) = 4 + 3 + 2 + f(1) = 4 + 3 + 2 + 1 + f(0) = 10, since f(0) = 0.

6. What is the minimum number of moves needed to solve Towers of Hanoi with 5 discs?

  1. 31
  2. 10
  3. 32
  4. 25
Show answer

A: 31. The recursive solution gives moves(n) = 2 × moves(n−1) + 1, which is 2ⁿ − 1. For n = 5 this is 31.

7. Which statement comparing recursion with iteration is correct?

  1. Recursion is always faster
  2. Recursion usually needs more memory than the equivalent loop
  3. Iteration cannot solve recursive problems
  4. Recursion never uses extra memory
Show answer

B: Recursion usually needs more memory than the equivalent loop. Each recursive call adds a stack frame, so memory use grows with depth. A loop normally reuses the same variables. Any recursive algorithm can be written iteratively.

8. A binary tree has root 4. Its left child is 2 (children 1 and 3) and its right child is 6. What does a recursive in-order traversal output?

  1. 4 2 1 3 6
  2. 1 3 2 6 4
  3. 4 2 6 1 3
  4. 1 2 3 4 6
Show answer

D: 1 2 3 4 6. In-order is left subtree, node, right subtree, giving 1 2 3 4 6. Option 1 is pre-order, option 2 is post-order and option 3 is breadth-first.

9. A recursive binary search halves the list on every call. About how many halvings reduce a list of 1024 items to a single item?

  1. 32
  2. 1024
  3. 10
  4. 512
Show answer

C: 10. 1024 = 2¹⁰, so halving 10 times reaches size 1. Binary search therefore has O(log n) depth.

10. What is the purpose of memoisation in a recursive Fibonacci function?

  1. To remove the base case
  2. To convert it to a loop automatically
  3. To store results already calculated so they are not recomputed
  4. To increase the stack size
Show answer

C: To store results already calculated so they are not recomputed. Storing earlier results, for example in a dictionary, means each fib(k) is calculated once. The time falls from O(2ⁿ) to O(n).

Keep going in Full Marks

Full Marks has 24 questions on Recursion and recursive algorithms (14 more than here), set out in practice levels. You also get notes and 3D models for all 6 subtopics of Algorithms and problem solving, plus games, timed exams and past papers.

On your exam board

All A-level Computer Science Year 13 topics