B2.4 Programming algorithms questions for IB Computer Science

181 questions on Programming algorithms, written by Augment in the style of IB exam papers. Six of them are below in full, to answer on paper.

  • IB Computer Science
  • Part of B2 Programming
  • SL and HL

Questions by paper and level

SL and HL questions are for students at either level. HL only questions cover what only HL students study.

PaperSL and HLHL onlyAll
Paper 211665181

Counted on

Questions to try

Written by Augment in the style of IB exams, not past papers. Answer them on paper, then check each one against its markscheme with a free account.

Question 1 · CS-00951Paper 2 · 6 marks

A fitness app stores a user's daily step counts in a list (Python) or array (Java) called steps, holding n values. The app uses the four functions/methods below.

Python

Python
def first_day(steps):
    return steps[0]

def total_steps(steps):
    total = 0
    for s in steps:
        total = total + s
    return total

def count_beaten(steps):
    count = 0
    for i in range(len(steps)):
        for j in range(len(steps)):
            if steps[i] > steps[j]:
                count = count + 1
    return count

def doublings(steps):
    k = 1
    count = 0
    while k < len(steps):
        k = k * 2
        count = count + 1
    return count

Java

Java
public static int firstDay(int[] steps) {
    return steps[0];
}

public static int totalSteps(int[] steps) {
    int total = 0;
    for (int s : steps) {
        total = total + s;
    }
    return total;
}

public static int countBeaten(int[] steps) {
    int count = 0;
    for (int i = 0; i < steps.length; i++) {
        for (int j = 0; j < steps.length; j++) {
            if (steps[i] > steps[j]) {
                count = count + 1;
            }
        }
    }
    return count;
}

public static int doublings(int[] steps) {
    int k = 1;
    int count = 0;
    while (k < steps.length) {
        k = k * 2;
        count = count + 1;
    }
    return count;
}
  1. (a)

    State the time complexity, in Big O notation, of each of the four functions/methods.

    4 marks

  2. (b)

    Identify which function/method would scale least well as the user keeps recording steps for several years, and outline why.

    2 marks

Question 2 · CS-00952Paper 2 · 5 marks

A school's timetabling program uses an algorithm with time complexity O(n2), where n is the number of students. Timetabling 200 students takes 3 seconds.

  1. (a)

    Calculate the approximate time needed to timetable 800 students, showing your working.

    2 marks

  2. (b)

    A rival program uses an O(n) algorithm that also takes 3 seconds for 200 students. State its approximate time for 800 students.

    1 mark

  3. (c)

    Explain why the times calculated in parts (a) and (b) are only estimates.

    2 marks

Question 3 · CS-00953Paper 2 · 6 marks

A weather buoy stores the last n water temperatures in a list (Python) or array (Java) called temps. Two functions/methods have been written to put the readings in reverse order.

Python

Python
def reversed_copy(temps):
    result = []
    for i in range(len(temps) - 1, -1, -1):
        result.append(temps[i])
    return result

def reverse_in_place(temps):
    left = 0
    right = len(temps) - 1
    while left < right:
        hold = temps[left]
        temps[left] = temps[right]
        temps[right] = hold
        left = left + 1
        right = right - 1

Java

Java
public static int[] reversedCopy(int[] temps) {
    int[] result = new int[temps.length];
    for (int i = 0; i < temps.length; i++) {
        result[i] = temps[temps.length - 1 - i];
    }
    return result;
}

public static void reverseInPlace(int[] temps) {
    int left = 0;
    int right = temps.length - 1;
    while (left < right) {
        int hold = temps[left];
        temps[left] = temps[right];
        temps[right] = hold;
        left = left + 1;
        right = right - 1;
    }
}
  1. (a)

    Outline what is meant by the space complexity of an algorithm.

    2 marks

  2. (b)

    State the space complexity of reversed_copy / reversedCopy and of reverse_in_place / reverseInPlace.

    2 marks

  3. (c)

    Both functions/methods have a time complexity of O(n). Explain why reverse_in_place / reverseInPlace is more suitable for the buoy, which has very little memory and stores a million readings.

    2 marks

Question 4 · CS-00954Paper 2 · 14 marks

A music streaming service checks whether a playlist contains the same song twice. Each song is identified by an integer ID, and a playlist is a list (Python) or array (Java) of IDs called ids.

Python

Python
def has_duplicate(ids):
    n = len(ids)
    for i in range(n - 1):
        for j in range(i + 1, n):
            if ids[i] == ids[j]:
                return True
    return False

Java

Java
public static boolean hasDuplicate(int[] ids) {
    int n = ids.length;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (ids[i] == ids[j]) {
                return true;
            }
        }
    }
    return false;
}
  1. (a)

    State the value returned when ids holds [12, 7, 33, 7].

    1 mark

  2. (b)

    Calculate the number of times the comparison ids[i] == ids[j] is made, in the worst case, for a playlist of 6 songs.

    2 marks

  3. (c)

    Deduce the worst-case time complexity of has_duplicate / hasDuplicate, explaining your answer.

    3 marks

  4. (d)

    Describe the best case for this function/method and state its time complexity.

    2 marks

  5. (e)

    The service now keeps every playlist sorted by ID.

    Construct, in Python or Java, a function/method has_duplicate_sorted / hasDuplicateSorted that returns whether a sorted playlist contains a repeated ID, using a single loop. For example, [7, 12, 12, 33] returns true and [7, 12, 33] returns false.

    4 marks

  6. (f)

    State the time complexity of your solution to part (e) and outline why it scales better than has_duplicate / hasDuplicate for long playlists.

    2 marks

Question 5 · CS-00955Paper 2 · 4 marks

A games company is comparing algorithms for its online leaderboard. Their time complexities are O(n2), O(1), O(nlog⁡n), O(log⁡n) and O(n).

  1. (a)

    List the five complexities in order, from the most scalable to the least scalable.

    2 marks

  2. (b)

    The leaderboard holds 1 048 576 (220) players. State the approximate number of steps an O(log⁡n) algorithm would take, assuming base 2.

    1 mark

  3. (c)

    State how many times more steps an O(n2) algorithm needs when the number of players doubles.

    1 mark

Question 6 · CS-00956Paper 2 · 4 marks

A bakery chain has n shops. For each shop, a 2D list (Python) or 2D array (Java) orders holds the number of orders on each of the 7 days of one week, so orders[i][d] is the number of orders at shop i on day d. The function/method below counts the days on which a shop received more than 100 orders.

Python

Python
def busy_days(orders):
    count = 0
    for i in range(len(orders)):
        for d in range(7):
            if orders[i][d] > 100:
                count = count + 1
    return count

Java

Java
public static int busyDays(int[][] orders) {
    int count = 0;
    for (int i = 0; i < orders.length; i++) {
        for (int d = 0; d < 7; d++) {
            if (orders[i][d] > 100) {
                count = count + 1;
            }
        }
    }
    return count;
}
  1. (a)

    State the number of times the comparison orders[i][d] > 100 is carried out when there are 50 shops.

    1 mark

  2. (b)

    A student says the time complexity is O(n2) because the function/method contains nested loops. Deduce the correct time complexity, explaining why the student is wrong.

    3 marks

Practice all 181 in Augment

Free with an account. Every question has its markscheme, and Augment keeps the ones you got wrong so you can come back to them.

The notes on B2.4 Programming algorithms

More questions in Programming

  1. B2.1Programming fundamentals
  2. B2.2Data structures
  3. B2.3Programming constructs
  4. B2.4Programming algorithmsThis page
  5. B2.5File processing

Check your answers in Augment

A free account opens all 181 questions on Programming algorithms with their markschemes, and keeps a record of what you have marked.