Algorithms

Section quiz

Contents

Contents

Algorithms

0 of 454 complete

Section quiz

Foundations: correctness and asymptotic analysis

Nine questions on contracts, invariants, induction, the notation, and what a cost claim actually promises.

Questions are shuffled within each part, and so are the answers. You can retake it as often as you like.

0 of 9 answered

Correctness

Question 1

A recursive procedure is given a whole number n, has base case n == 0, and calls itself with n - 2. What is the state of its termination argument?

Question 2

You have shown that a loop invariant holds before the first pass and that every pass preserves it. What else must be shown before the algorithm can be called correct?

Question 3

A procedure is meant to return the largest value in a non-empty list of whole numbers. It starts a variable at 0 and replaces it whenever it meets a larger entry. Which statement is right?

Question 4

Someone proposes this invariant for insertion sort's outer loop: 'positions 0 to i - 1 are in increasing order'. What is wrong with it?

Cost

Question 5

A recursive procedure sums a list by adding the first entry to the sum of the rest, allocating nothing. What is its auxiliary space?

Question 6

A problem states that n is at most 20 and asks for the best arrangement of n items, with a one second limit. What does that constraint suggest?

Question 7

A growable array offers appends with amortised cost O(1) and worst case O(n) for a single append. Which claim does that support?

Question 8

An algorithm's running time satisfies T(n) = O(n^2). Which conclusion is justified?

Question 9

What exactly must you produce to establish that f(n) = O(g(n))?

9 questions left to answer.