← All problemsFree · Python · 50 min

Algorithm design / guarantees under an adversary

The Coat Check

Pile every coat on one hook by choosing ticket numbers, find the exact collision chance of the Carter-Wegman family, then write a hash family that an adversary who reads your code still cannot beat.

0 / 4 graded parts passSaved in this browser · loading saved work…
Jump to your work ↓

A theatre's coat check has m hooks, numbered 0 to m−1. Each coat goes on the hook a formula computes from its ticket number. Finding a coat means searching its hook, so a hook with many coats is slow. A group of pranksters can buy whichever ticket numbers they like, and they know the formula. Can they pile every coat on one hook?

h(x) = (5x + 3) mod 8, 8 ticketshook 04125hook 130hook 2hook 3hook 453hook 51866hook 67hook 712finding a coat searches its hook: the busiest hook holds 2
One formula, eight tickets: where each coat hangs.

The coat check's defence is to keep a family of formulas and pick one at random each night, after the tickets are sold. The pranksters know the family but not tonight's pick. A family is universal if any two different tickets share a hook with probability at most 1/m over that pick.

In code, a family is make_hash(m, seed): the seed is the night's random choice, and the result is a function from ticket numbers to hooks.

Grading

  • Parts A to C are checked exactly: every formula in a family is tried.
  • Part D plays an adversary who reads your code but not the seed. It picks tickets designed to collide under common mistakes, simulates 400 nights for m=16 and m=64, and fails your family if any pair shares a hook on more than 4/m of the nights.
  • No timing decides a grade.

Background: Universal and Perfect Hashing.

easy · Part A

One hook, eight coats

The formula is h(x)=xmod64. Give 8 different ticket numbers below 220 that all hang on the same hook.

Explore in the sandbox

Experiment here, then copy a rule into the editor and test its guarantees.