IOI · feeder round mock

Informatics mock paper 1.

Modelled on a written first-round informatics olympiad paper of the kind several countries use before their programming rounds: short questions on tracing code, logic, counting and complexity, then hand-worked algorithm questions; the medal bands are only a rough guide.

90minutes50marksNo calculator
  • Answer all questions. You have 90 minutes.
  • Section A: choose one answer for each question. Section B: show your working and reasoning.
  • Code is written in Python. Pseudocode in Section B uses the same ideas in plain words.
  • You may use scrap paper to trace programs by hand. No computer or calculator.
  • Total: 50 marks (Section A 24, Section B 26).

The clock starts when you press Start. Multiple-choice answers are marked for you; for written answers you get the mark scheme and give yourself the marks. Your answers stay on this device.

Feeder round mock paper 1
TheLucidSTEM · Informatics olympiad feeder round mock · 90 minutes · 50 marks · No calculator
Name: ______________________   Date: ____________ · MARK SCHEME
Data sheet

Python reminders

  • range(a, b) gives a, a + 1, ..., b - 1; range(a, b, s) goes up (or down, if s is negative) in steps of s and stops before b.
  • // is whole-number division (rounding down), % is the remainder, ** is a power.
  • & is bitwise AND: each bit of the result is 1 only if that bit is 1 in both numbers.
  • Strings and lists are indexed from 0: w[0] is the first character.
  • continue skips the rest of the loop body and goes on to the next value.

Section A: multiple choice

Suggested time 35 minutes. Two marks each, no penalty for wrong answers.

1[2]
What does this program print?
s = 0
for i in range(1, 8):
    if i % 3 == 0:
        continue
    s += i
print(s)
2[2]
What does this program print?
w = "OLYMPIAD"
r = ""
for k in range(len(w) - 1, -1, -2):
    r += w[k]
print(r)
3[2]
A, B and C are each true or false. For how many of the 8 possible combinations is the expression
(A or B) and not (B and C)
true?
4[2]
work() takes constant time. As $n$ grows, how does the total number of calls to work() grow?
i = n
while i > 0:
    for j in range(i):
        work()
    i = i // 2
5[2]
How many times is count increased when n = 8?
count = 0
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            count += 1
6[2]
Consider this function. How many calls to h are made in total (including the first one) when h(10) is evaluated?
def h(n):
    if n == 0:
        return 1
    if n % 2 == 1:
        return 2 * h(n - 1)
    return h(n // 2) ** 2
7[2]
A robot walks on a 4 by 4 grid of squares, along the grid lines, from the top-left corner to the bottom-right corner. Each move goes one unit right or one unit down. The point in the exact centre of the grid is blocked. How many different routes are there?
8[2]
What does this program print? (& is bitwise AND.)
def f(x):
    c = 0
    while x > 0:
        x = x & (x - 1)
        c += 1
    return c
print(f(200))
9[2]
The numbers 1, 2, 3, 4 are pushed onto a stack in that order. At any moment you may pop the top number and write it down, and every number is popped by the end. Which output order is impossible?
10[2]
Roads join five towns with these lengths (km): A–B 4, A–C 1, B–C 2, B–D 5, C–D 8, D–E 3, B–E 9, C–E 12. Roads can be used in both directions. What is the length of the shortest route from A to E?
11[2]
The keys 50, 30, 70, 20, 40, 60, 80, 35 are inserted in that order into an empty binary search tree (smaller keys go left). In what order does a post-order traversal (left subtree, right subtree, then the node) visit the keys?
12[2]
A hash table has 7 slots, numbered 0 to 6. A key k goes into slot k mod 7; if that slot is full, it tries the next slot (wrapping round from 6 to 0) until it finds an empty one. The keys 10, 3, 17, 24, 5 are inserted in that order. In which slot does 5 end up?

Section B: structured questions

Suggested time 55 minutes, about 18 minutes per question. Explain your reasoning: a correct method earns marks even if the final number is wrong.

13The longest rising run of scores[9 marks]

A player's scores in ten games were

3, 8, 2, 5, 9, 4, 6, 10, 1, 7

An increasing subsequence is a choice of some of the games, kept in their original order, whose scores strictly increase (for example 2, 5, 9). We want the longest one.

For each position $i$ (1 to 10), let $L[i]$ be the length of the longest increasing subsequence that ends at game $i$. The algorithm is:

for i = 1 to n:
    L[i] = 1
    for j = 1 to i - 1:
        if a[j] < a[i]:
            L[i] = max(L[i], L[j] + 1)
answer = the largest value in L

(a) Copy and complete the table of $L[i]$ for all ten games.

[3]

(b) State the length of the longest increasing subsequence and write down one such subsequence.

[2]

(c) How many times is the comparison a[j] < a[i] made for these 10 games? Give a formula for $n$ games and the time complexity of the algorithm.

[2]

(d) The table $L$ gives the length but not the subsequence. Explain what extra information the algorithm should store so that one longest subsequence can be printed at the end, and how it is printed.

[2]

14The longest dry spell[9 marks]

A weather station records the rainfall on each of $n$ days as whole numbers of millimetres $R[1], R[2], \dots, R[n]$, all zero or more. A spell is a run of consecutive days. A spell is dry if its total rainfall is at most $K$. We want the length of the longest dry spell.

Example: $n = 9$, $K = 5$, rainfall 2, 1, 0, 3, 1, 1, 4, 0, 2.

(a) For the example, find the length of the longest dry spell and say which days it covers.

[2]

(b) A simple method checks every spell. How many spells are there for $n$ days (give the number for $n = 9$)? What is the time complexity of this method if the total of each spell is found by adding up its days one by one?

[2]

(c) Design an algorithm that finds the answer in $O(n)$ time. Write it in pseudocode or clear numbered steps, and explain briefly why it takes $O(n)$ time.

[4]

(d) Suppose the numbers could be negative (say they record a change in reservoir level instead of rainfall). Give a short example that shows your method from (c) can then give the wrong answer.

[1]

15The cost of counting in binary[8 marks]

A counter is stored as a row of binary digits (bits) and starts at 0. The operation INCREMENT adds 1 to it, by the usual method: going from the right, change every 1 to 0 until you reach a 0, and change that 0 to 1. The cost of an operation is the number of bits it changes. For example, going from 0101 (5) to 0110 (6) changes 2 bits, so it costs 2.

(a) What is the cost of the increment from 7 to 8?

[1]

(b) The counter starts at 0 and is incremented 16 times, ending at 16. Find the total cost of the 16 increments.

[2]

(c) A single increment can cost as much as the number of bits. Show that $N$ increments starting from 0 cost less than $2N$ in total, so the average cost per increment is less than 2.

[3]

(d) A DECREMENT operation (subtract 1) is now also allowed, with cost again the number of bits changed. Show that the average cost per operation is no longer bounded by a constant: describe a sequence of $m$ operations, for any $m$, whose total cost is about $mk$ for a counter of $k$ bits.

[2]

TheLucidSTEM practice paper. Questions are original and written in the style of a first national round; this is not an official olympiad paper.