Practice questions
10 test questions · 5 written questions · 15 total
Each question is followed by its answer and explanation. Hard questions also include a starting hint and smaller reasoning steps.
Test questions
Choose one option. Cost questions state their model and whether the bound is tight, expected or worst-case. Here lg means log₂, and heap positions start at 1.
Question 1 · medium · course question
A proposed optimisation runs faster but gives a wrong result on a legal input. What should you conclude?
- use it because speed is the only goal
- average its answer with another result
- it is not a correct replacement
- remove that input from the tests without changing the specification
Answer: option C. An optimisation must preserve the required behaviour on all legal inputs. Speed does not repair a correctness failure.
Question 2 · medium · course question
A program performs n membership tests on an ordinary unsorted list of n items, and each test scans the whole list. What is the total work?
- Θ(n²)
- Θ(n)
- Θ(log n)
- Θ(1)
Answer: option A. There are n tests, each taking Θ(n), so the work multiplies to Θ(n²). A suitable set can reduce expected lookup cost when its assumptions and setup costs are included.
Question 3 · medium · course question
A report says “algorithm A is best.” What information is most useful to add?
- a more impressive adjective
- the author’s favourite colour
- only the code font
- the objective, input assumptions and measured or proved costs
Answer: option D. “Best” depends on what matters: time, memory, deadlines or another objective. State the conditions and evidence supporting the choice.
Question 4 · medium · course question
A method has low average time but occasional long pauses. Which requirement makes those pauses particularly important?
- printing its source code
- meeting a strict per-request deadline
- having short variable names
- using an odd number of inputs
Answer: option B. A strict deadline concerns each request, so occasional slow cases matter even when the average is small. Expected or amortised bounds are not worst-case deadline guarantees.
Question 5 · medium · course question
Which test is especially useful when changing a sorting implementation?
- only one already sorted list
- only lists of length 100
- empty, single-item, repeated-key, sorted and reverse-sorted cases
- only the timing of a random list
Answer: option C. These cases exercise boundaries and common assumptions. They improve testing coverage, though testing alone is not a proof for every input.
Question 6 · medium · course question
A faster method uses much more memory. How should the choice be justified?
- compare both resources against the application’s limits
- ignore memory because only time counts
- always select the larger program
- assume every device has unlimited storage
Answer: option A. The suitable method depends on available memory and acceptable time. A speed improvement may be unusable on a memory-limited device.
Question 7 · medium · course question
Which claim is supported by testing an algorithm successfully on 1,000 randomly chosen inputs?
- it is proved correct on all inputs
- its worst-case time is known
- no edge cases remain
- it passed those 1,000 tests
Answer: option D. The observations support exactly the tested cases. A general correctness claim additionally needs reasoning about all legal inputs.
Question 8 · medium · course question
A preprocessing step costs 1,000 operations and each later query costs 10. Without preprocessing, each query costs 100. For 20 queries, which total is smaller?
- no preprocessing: 2,000 versus 3,000
- preprocessing: 1,200 versus 2,000
- they tie at 2,000
- preprocessing: 200 versus 1,000
Answer: option B. With preprocessing: 1,000+20×10=1,200. Without it: 20×100=2,000. Include both setup and query work.
Question 9 · medium · course question
For a new problem, which order is most useful?
- time arbitrary code, then decide what result was needed
- choose the shortest-looking code, then ignore its assumptions
- specify the required result, justify a method, then evaluate its resource costs
- select a complexity label before defining input size
Answer: option C. A precise specification supports correctness reasoning; only then can performance comparisons refer to the right task and input model.
Question 10 · hard
For n distinct keys and the standard implementations, which statement is FALSE?
- array heapsort can sort in place
- mergesort has Θ(n log n) worst-case time
- comparison sorting needs Ω(n log n) comparisons on average over uniformly random permutations
- random-pivot, two-way quicksort has Θ(n log n) worst-case time
In simpler words: Distinguish expected cost from worst-case cost.
Starting hint: Random choices can be unlucky even when their average is good.
Answer: option D. Random pivots give expected Θ(n log n) work on every fixed distinct-key input. Some pivot sequences still cause Θ(n²) work. Expected performance is not a worst-case guarantee.
Step by step
- Balanced pivot splits give about log n levels, with linear work per level.
- An unlucky sequence of extreme pivots leaves n−1,n−2,… items. Summing those costs gives Θ(n²), so (d) is false.
Written questions
Read each question together with its explanation, trace or proof. Numbering continues from the test questions.
Question 11 · easy
Give three reasons the course spends so much time on sorting.
Answer & reasoning
computers spend a large share of their time sorting; it is the best-studied problem, with many algorithms to compare; and most big ideas (divide and conquer, randomisation, lower bounds) can be taught through it. Also, once data is sorted many other problems become easy.
Question 12 · medium
Give two ways to find the k-th smallest of n numbers, one based on sorting and one on a heap, with their costs.
Answer & reasoning
sort and read position k: O(n log n). Or build a min-heap in O(n) and extract the minimum k times: O(n + k log n), which is better when k is small.
Question 13 · medium
Two sets of sizes m and n, with 2 ≤ m ≤ n, are supplied as unsorted arrays. Give a comparison-based method and an expected-time hashing method to test disjointness.
Answer & reasoning
Sort the smaller set in O(m log m), then binary-search each of the n larger-set items in it in O(n log m), for O((m + n) log m) total. Alternatively hash the smaller set and scan the larger in O(m + n) expected time under suitable hashing assumptions. Empty sets are immediately disjoint; for m = 1 a linear scan suffices.
Question 14 · medium
Why might quicksort run faster than heapsort on a particular machine even though both have n log n behaviour in the relevant case?
Answer & reasoning
Big-O hides constants and memory-access effects. Quicksort often has good locality and a simple inner loop, while heap operations jump between array positions. The actual comparison depends on input, implementation and hardware; measure it. Randomised quicksort is expected Θ(n log n) under the appropriate distinct-key or duplicate-handling assumptions and can have a quadratic worst case; heapsort has an O(n log n) worst-case guarantee.
Question 15 · hard
Prove that pairing the smallest number with the largest, the second smallest with the second largest, and so on, minimises the largest pair sum (the problem of the day from Lecture 7).
In simpler words: Pair very small values with very large values to control the worst pair.
Starting hint: Compare an arbitrary pairing with one that pairs the two extremes.
Answer & reasoning
Step by step
- Suppose the smallest a pairs with x and the largest z pairs with y.
- Replace these pairs by (a,z) and (x,y). Since a ≤ y and x ≤ z, both new sums are at most the old sum y+z.
- The maximum cannot increase. Fix the extremes together and apply the same argument to the remaining values. This is an exchange argument: improve the structure without worsening the objective.
sort the numbers a₁ ≤ … ≤ a₂ₙ. Take any optimal pairing in which a₁ is paired with some x and a₂ₙ with some y, x ≠ a₂ₙ. Re-pair them as (a₁, a₂ₙ) and (x, y). Since a₁ ≤ y, a₁ + a₂ₙ ≤ y + a₂ₙ; since x ≤ a₂ₙ, x + y ≤ a₂ₙ + y. Both new sums are at most the old pair sum a₂ₙ + y, so the maximum does not increase. Repeat the argument on the remaining numbers; by induction the fully sorted pairing is optimal.