Bitpath

Boss lesson · Unit 04

Build a hash map

Write get and put with chaining, then see how keys can go wrong

12 exercisesabout 3 min

What you practise

  • put and get with chaining
  • Why keys must not change
  • Map key types per language

Try a question from this lesson

Pick one

A map already holds the pair ("a", 1). You store ("a", 5). What should the map hold afterwards?

The idea

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Keys must not change

A pair's bucket is chosen from its key at the moment it is stored. If the key changes afterwards, its hash changes too, but the pair stays where it was.

A lookup with the new value goes to a different bucket and finds nothing. So a key must be something that cannot change, such as a string or a number.

Python notePython enforces this: lists, dicts and sets are "unhashable" and cannot be keys. A tuple can, because it cannot be modified.

JavaScript noteMap compares object keys by identity, not by contents. Two separate arrays with equal items are two different keys.

TypeScript noteMap<string, number> fixes the key and value types. A Map<number[], string> compiles, but its lookups need the very same array object.

10 more exercises in the app

  • 1× order the lines
  • 1× fill the blanks
  • 3× multiple choice
  • 3× type the answer
  • 1× true or false
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy