Skip to content

Break it

Every algorithm has inputs it hates. Find them: knowing what breaks an algorithm is how you learn what it relies on.

01 · Quicksort

Make quicksort slow

This quicksort always takes the last value as its pivot. On a random order of 10 values it makes about 24 comparisons. Give it 10 values that make it do the most work it ever can: 45 comparisons.

Whole numbers from 5 to 99, separated by commas or spaces.

Need a hint?

A partition is cheap to recurse on when the pivot splits the values evenly. What order makes the last value the worst possible pivot, every time?

02 · Greedy

Fool the greedy cashier

A greedy cashier makes change by always taking the largest coin that still fits. With real coins that gives the fewest coins. Find an amount where it doesn’t.

Coins

A whole number from 1 to 60.

Need a hint?

Try amounts just above a big coin. Taking it might leave a remainder that only small coins can make, when two middle-sized coins would have done.

03 · Hashing

Flood the hash table

This hash table has 8 buckets and puts key k in bucket k mod 8. Keys that land in the same bucket form a chain, and a lookup walks the chain. Choose 8 keys that make one chain as long as it can be.

Whole numbers from 0 to 999, separated by commas or spaces.

Need a hint?

Two keys share a bucket when they leave the same remainder after dividing by 8.

Attackers do this to real servers: send keys that all collide and every lookup slows to a scan. That is why real hash tables pick a random, secret (salted) hash function when they start, so nobody can predict which keys collide.