Home › Learn › World 3: Hash Harbor
FREELocker 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.length | How 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 / get | O(1) average |
| Worst case | O(n) if everything collides |
| Space | O(n) |
How the stars are earned
- Correct list on every test
- At most 2 visits per order
- 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.
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
- Duplicate Parcel: Hash set membership, O(n)
- Frequency Board: Counting with a hash map, O(n)
- Two Sum Harbor: Hash map complement lookup, O(n)
- Anagram Crates: Letter counts in a hash map, O(n)
- Longest Chain: Hash set sequence starts, O(n)
- Smuggler Twins: Prefix sums + hash map, O(n)