Question bank › A-level › A-level Computer Science › Theory of computation

Classification of algorithms, Big-O, halting problem and Turing machines

10 questions47 marksabout 55 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Standard4 marks

(a) State the time complexity of the following algorithm in terms of n. [1]

FOR i ← 1 TO n
  FOR j ← 1 TO n
    OUTPUT i * j
  ENDFOR
ENDFOR

(b) State the time complexity of a binary search on a sorted list of n items. [1]

(c) An algorithm with time complexity O(n²) takes 4 ms for n = 1000. Estimate the time it takes for n = 3000. Show your working. [2]

2Hard6 marks

A Turing machine has the transition table below. Symbols are 0, 1 and _ (blank). The tape is blank on both sides of the input. The head starts on the leftmost symbol of the input in state q0. H is the halting state. Each rule is (new state, symbol written, direction).

StateRead 0Read 1Read _
q0(q0, 0, R)(q0, 1, R)(q1, _, L)
q1(H, 1, R)(q1, 0, L)(H, 1, R)

(a) The tape initially holds 1011. Give the tape contents when the machine halts. [1]

(b) State what the machine does to the binary number on the tape. [1]

(c) State the number of transitions the machine makes on the input 1011, including the one that enters H. Show your working. [2]

(d) Explain why a Turing machine is a useful model of computation. [2]

3Standard4 marks

(a) State what is meant by the halting problem. [1]

(b) State the significance of the halting problem for computer scientists. [1]

(c) Distinguish between a problem that is intractable and a problem that is unsolvable. [2]

Stuck on Classification of algorithms, Big-O, halting problem and Turing machines?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Core3 marks

(a) A music app can play 7 different songs in any order, with each song played exactly once. Calculate how many different orders are possible. [1]

(b) A function is defined as $f: \mathbb{N} \rightarrow \mathbb{N}$, $f(n) = 3^n$. State the domain of $f$. [1]

(c) Classify each of the following functions as linear, polynomial, exponential or logarithmic. [1]

  • $g(n) = 5n^3 + 2n$
  • $h(n) = \log_2 n$
5Standard4 marks

Two algorithms check whether a list L of n integers contains any repeated value. Every value in L is an integer from 0 to 999.

Algorithm A

dup ← FALSE
FOR i ← 0 TO n - 2
  FOR j ← i + 1 TO n - 1
    IF L[i] = L[j] THEN
      dup ← TRUE
    ENDIF
  ENDFOR
ENDFOR

Algorithm B

seen ← array of 1000 Booleans, all set to FALSE
dup ← FALSE
FOR i ← 0 TO n - 1
  IF seen[L[i]] = TRUE THEN
    dup ← TRUE
  ELSE
    seen[L[i]] ← TRUE
  ENDIF
ENDFOR

(a) Calculate how many times Algorithm A carries out the comparison L[i] = L[j] when n = 50. [1]

(b) State the time complexity of Algorithm A and of Algorithm B, using Big-O notation. [2]

(c) Explain one advantage of Algorithm A compared with Algorithm B. [1]

6Standard4 marks

The graph shows how the time taken by four algorithms, A, B, C and D, grows as the input size n increases. The algorithms have time complexities O(1), O(log n), O(n) and O(2n), in no particular order.

(a) Identify which curve, A, B, C or D, matches each time complexity. [2]

(b) The algorithm below is run with n = 1000.

count ← 0
WHILE n > 1
  n ← n DIV 2
  count ← count + 1
ENDWHILE
OUTPUT count

State the value output, and state which curve on the graph best represents the time complexity of this algorithm. [2]

Input size, nTime takenABCD
7Hard6 marks

A programmer measures the time taken by two algorithms, P and Q, for different input sizes n.

Algorithm P: nTime (s)Algorithm Q: nTime (s)
10000.2200.5
20000.8211.0
40003.2222.0

(a) State the time complexity of Algorithm P in Big-O notation. Justify your answer using the data. [2]

(b) Estimate the time Algorithm P would take when n = 10 000. [1]

(c) Algorithm Q has time complexity O(2n). It is moved to a computer that runs 1000 times faster. Calculate the largest value of n that Algorithm Q can now process in 1 second or less. Show your working. [2]

(d) Using your answer to (c), explain what this shows about the limits of computation. [1]

8Hard6 marks

A delivery van leaves a depot, visits each of a set of shops exactly once and returns to the depot. The company wants the route with the shortest total distance. A route and its reverse are counted as different routes.

(a) Calculate the number of possible routes when there are 10 shops. [1]

(b) A computer can check one million routes per second. Calculate how many days it would take to check every route when there are 15 shops. Give your answer to 3 significant figures. [2]

(c) Explain why the problem of finding the shortest route is described as intractable. [1]

(d) Describe how a heuristic approach could be used to find a route for this problem, and explain why the company might accept it. [2]

9Stretch4 marks

Suppose a programmer claims to have written a function Halts(P, I) that, for any program P and input I, always returns TRUE if P would eventually stop when run on I, and returns FALSE if P would run forever. A second program is written that uses it:

PROCEDURE Contrary(P)
  IF Halts(P, P) = TRUE THEN
    WHILE TRUE
      // loop forever
    ENDWHILE
  ELSE
    STOP
  ENDIF
ENDPROCEDURE

(a) Contrary is run with its own code as the input, i.e. Contrary(Contrary). Explain why this shows that the function Halts cannot exist. [3]

(b) Another programmer suggests deciding whether a program halts by running it for 24 hours and returning FALSE if it has not stopped by then. Explain why this does not solve the halting problem. [1]

10Hard6 marks

The state transition diagram shows a Turing machine with tape alphabet {0, 1, □}, where □ is the blank symbol. Each label shows symbol read / symbol written, direction moved. The machine starts in state S0 with the read-write head on the leftmost symbol of the input; every square to the right of the input is blank. SH is the halting state.

(a) The tape initially holds 1101. Give the sequence of states the machine passes through and the contents of the tape when it halts. [2]

(b) State the contents of the tape when the machine halts if the input is 1001. [1]

(c) Write the transition from S1 to SH as a transition function, in the form δ(state, symbol read) = (new state, symbol written, direction). [1]

(d) State the purpose of this Turing machine. [1]

(e) Explain why the machine needs two different non-halting states, S0 and S1. [1]

S0S1SHstart0 / 0, R0 / 0, R1 / 1, R1 / 1, R□ / 0, L□ / 1, L

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session