CODE THE HERO

Home › Learn › World 3: Hash Harbor

FREE

Locker Room

Hash map put and get, O(1) each

Level 3.1 of Code the Hero, in Hash Harbor: hash maps: find anything in one visit.

The idea

A hash function turns a key into a locker number, so put and get go straight to one locker. Collisions share a locker as a short chain.

The mission

Welcome to Hash Harbor! Every parcel has a label, and the label decides its locker. lockers.put(label, value) stores a parcel. lockers.get(label) walks straight to the right locker. No searching the whole wall: one visit per order. That's the magic of hashing.

Goal: Store every parcel under its label, then submit a list with the value for each order, in order.

The commands you get

You write real Python, Java or JavaScript. These are the moves the hero understands in this level:

parcels.lengthHow many parcels arrived
parcels.key(i) / parcels.value(i)Label and value of parcel i
lockers.put(key, value)Store a value under a key
lockers.get(key)Open the key's locker and return its value (1 visit)
orders.length / orders.get(i)The labels customers ask for
submit(list)Hand in the list of values

What it costs

put / getO(1) average
Worst caseO(n) if everything collides
SpaceO(n)

How the stars are earned

  1. Correct list on every test
  2. At most 2 visits per order
  3. 1 locker visit per order

Your code is also checked on hidden inputs, so it has to work for any case, not only the one on screen.

▶ Play this level free

No sign-up. Levels open in order, so if this one is still locked you start at the first level that isn't.

More in Hash Harbor