- Subjects
- Computer Science
- Question bank
- B2.4 Programming algorithms
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.
| Paper | SL and HL | HL only | All |
|---|---|---|---|
| Paper 2 | 116 | 65 | 181 |
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.
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
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 countJava
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;
}- (a)
State the time complexity, in Big O notation, of each of the four functions/methods.
4 marks
- (b)
Identify which function/method would scale least well as the user keeps recording steps for several years, and outline why.
2 marks
A school's timetabling program uses an algorithm with time complexity , where n is the number of students. Timetabling 200 students takes 3 seconds.
- (a)
Calculate the approximate time needed to timetable 800 students, showing your working.
2 marks
- (b)
A rival program uses an algorithm that also takes 3 seconds for 200 students. State its approximate time for 800 students.
1 mark
- (c)
Explain why the times calculated in parts (a) and (b) are only estimates.
2 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
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 - 1Java
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;
}
}- (a)
Outline what is meant by the space complexity of an algorithm.
2 marks
- (b)
State the space complexity of
reversed_copy/reversedCopyand ofreverse_in_place/reverseInPlace.2 marks
- (c)
Both functions/methods have a time complexity of . Explain why
reverse_in_place/reverseInPlaceis more suitable for the buoy, which has very little memory and stores a million readings.2 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
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 FalseJava
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;
}- (a)
State the value returned when
idsholds[12, 7, 33, 7].1 mark
- (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
- (c)
Deduce the worst-case time complexity of
has_duplicate/hasDuplicate, explaining your answer.3 marks
- (d)
Describe the best case for this function/method and state its time complexity.
2 marks
- (e)
The service now keeps every playlist sorted by ID.
Construct, in Python or Java, a function/method
has_duplicate_sorted/hasDuplicateSortedthat 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
- (f)
State the time complexity of your solution to part (e) and outline why it scales better than
has_duplicate/hasDuplicatefor long playlists.2 marks
A games company is comparing algorithms for its online leaderboard. Their time complexities are , , , and .
- (a)
List the five complexities in order, from the most scalable to the least scalable.
2 marks
- (b)
The leaderboard holds 1 048 576 () players. State the approximate number of steps an algorithm would take, assuming base 2.
1 mark
- (c)
State how many times more steps an algorithm needs when the number of players doubles.
1 mark
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
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 countJava
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;
}- (a)
State the number of times the comparison
orders[i][d] > 100is carried out when there are 50 shops.1 mark
- (b)
A student says the time complexity is 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.
More questions in Programming
- B2.1Programming fundamentals
- B2.2Data structures
- B2.3Programming constructs
- B2.4Programming algorithmsThis page
- B2.5File processing
More on Programming algorithms
- B2.4 Programming algorithms notesWhat Programming algorithms covers, explained, with its key terms.
- IB Computer Science question bankEvery Computer Science topic counted, and one question from each with its markscheme.
- Computer Science exam formatEach Computer Science paper, how long it is and its share of the grade.
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.