Final exam
Data Structures and Algorithms
Before you start
- 20 questions, drawn at random from a pool of 39.
- Pass mark: 70%. Your final score is 30% practice + 70% exam.
- No time limit, but finish within 3 hours. You can move between questions before you submit.
- Failed? Review the lessons and try again: every attempt gets a new set of questions.
Question 1 of 20Choose one
What does O(n) time complexity tell you about an algorithm?
Question 2 of 20Choose one
What is the Big O of 4n² + 100n log n + 5,000?
Question 3 of 20Put in order
Order these complexities from fastest-growing to slowest-growing.
Drag the items, or use the arrow buttons, to put them in order.
- O(n log n)
- O(2ⁿ)
- O(n²)
- O(n!)
- O(log n)
- O(n)
Question 4 of 20Work it out
A linear O(n) algorithm processes 2 million records in 3 seconds. Roughly how many seconds will it take for 8 million records?
Question 5 of 20Match the pairs
For each loop structure, pick the matching growth rate.
Tap an answer, then tap the place where it belongs (or drag it there).
- A single loop that doubles i from 1 until it reaches n
- Two nested loops, each over all n elements
- A loop over n elements followed by a separate loop over m elements
- Accessing arr[k] for a known index k
Question 6 of 20True or false
Big O notation can only be used to describe the worst case of an algorithm.
Question 7 of 20Spot the mistake
A function checks every element of an array of size n, and for each one calls arr.indexOf on the same array. A student annotated the cost of each line. Tap the two annotations that are wrong.
Question 8 of 20Choose one
What does every correct recursive function need in order to terminate?
Question 9 of 20Match the pairs
Pair each recurrence relation with the running time it solves to.
Tap an answer, then tap the place where it belongs (or drag it there).
- T(n) = T(n/2) + O(1)
- T(n) = 2T(n/2) + O(n)
- T(n) = T(n − 1) + O(n)
- T(n) = T(n − 1) + O(1)
Question 10 of 20Choose one
How much call-stack space does a recursive factorial(n) use at its deepest point?
Question 11 of 20Sort into groups
Sort these operations by their cost.
Tap an item, then tap its group (or drag it there).
Question 12 of 20Choose one
Why are appends to a dynamic array described as amortized O(1)?
Question 13 of 20True or false
Growing a dynamic array by a fixed 10 slots each time it fills up keeps appends amortized O(1).
Question 14 of 20Choose one
You push A, B, C onto a stack, pop once, then push D. What does pop return next?
Question 15 of 20What would you do?
What would you do?
A web server must handle incoming requests fairly, in the order they arrive, and may hold up to 100,000 waiting requests. Which implementation is best?
Question 16 of 20Work it out
A hash table has 1,000 buckets and resizes (doubling its buckets) as soon as its load factor exceeds 0.75. What is the largest number of entries it can hold before the first resize?
Question 17 of 20Select all that apply
Which statements about hash tables are true? Select all that apply.
Select all that apply.
Question 18 of 20Choose one
Which approach finds whether an array contains any duplicate value in O(n) average time?
Question 19 of 20Work it out
What is the maximum number of comparisons binary search needs on a sorted array of 1,024 elements? (log₂ 1,024 = 10.)
Question 20 of 20True or false
Binary search requires the data to be sorted and to support fast access to any position.
Question 21 of 20Spot the mistake
Here is a binary search over a sorted array with an inclusive range. Tap the line that contains a bug.
Question 22 of 20Match the pairs
Match each sorting algorithm to its key characteristic.
Tap an answer, then tap the place where it belongs (or drag it there).
- Insertion sort
- Merge sort
- Quicksort
- Selection sort
- Counting sort
Question 23 of 20Choose one
Why can no comparison-based sorting algorithm guarantee better than O(n log n) worst-case time?
Question 24 of 20Sort into groups
Sort these algorithms by whether they are typically in place (O(1) or O(log n) extra space) or need O(n) extra space.
Tap an item, then tap its group (or drag it there).
Question 25 of 20Fill in the blanks
Fill in the blanks
In JavaScript, to sort an array of numbers in ascending order you should call nums.sort((a, b) => a b), because without a comparator sort compares elements as .
Question 26 of 20Work it out
Insert the keys 1, 2, 3, 4, 5, 6 in that order into an empty, non-balancing binary search tree. What is the height of the tree, counting edges on the longest root-to-leaf path?
Question 27 of 20Choose one
Which traversal of a binary search tree visits the keys in sorted order?
Question 28 of 20Put in order
A BST has root 40; 40's left child is 20 and right child is 60; 20 has children 10 and 30; 60 has a left child 50. Put the keys in postorder.
Drag the items, or use the arrow buttons, to put them in order.
- 60
- 40
- 50
- 20
- 10
- 30
Question 29 of 20Work it out
In an array-based binary heap with the root at index 0, what is the index of the right child of the node at index 6?
Question 30 of 20Choose one
In a min-heap, after removing the root and moving the last element to the top, why is it swapped with the smaller of its two children during sift-down?
Question 31 of 20Sort into groups
Sort these tasks by the structure that handles them most efficiently.
Tap an item, then tap its group (or drag it there).
Question 32 of 20Work it out
An undirected graph has 500 vertices and 2,000 edges. An adjacency matrix stores one cell for every ordered pair of vertices. How many cells does it have?
Question 33 of 20Choose one
Which algorithm finds the path with the fewest edges between two vertices in an unweighted graph?
Question 34 of 20Select all that apply
Which tasks are typical uses of depth-first search? Select all that apply.
Select all that apply.
Question 35 of 20True or false
With an adjacency list, both BFS and DFS run in O(V + E) time.
Question 36 of 20Choose one
What two properties make a problem a good fit for dynamic programming?
Question 37 of 20Work it out
With coins worth 1, 5 and 6, what is the minimum number of coins needed to make exactly 10?
Question 38 of 20What would you do?
What would you do?
You need to choose the maximum number of non-overlapping meetings for one conference room from a list of 10,000 requests, each with a start and end time. Which approach is correct and efficient?
Question 39 of 20Choose one