All puzzles › Hash Maps and Sets
Hash Maps and Sets Practice Problems
52 puzzles (16 easy, 30 medium, 6 hard), 17 free, in Python, JavaScript and C++.
A hash map answers one question instantly: have I seen this before, and what did I store with it? That single ability turns a surprising number of slow solutions into fast ones. These problems are the classic uses: counting how often things appear, grouping items that share a key, and finding a matching partner without scanning for it.
How to spot a hash maps and sets problem
The signal is a loop inside a loop where the inner one is searching for something. If the inner loop is asking "is there a value that completes this one?", remember what you have passed in a map or a set and ask it instead. You trade memory for a whole pass over the data.
New to it? Read the lesson first: Hash Maps & Sets.
The puzzles, easiest first
- A Bag With a Fixed Number of Pockets (Easy, free): A model needs a fixed-length vector, but the vocabulary of real text never stops growing.
- A Mask in a Handful of Numbers (Easy, free): A segmentation mask marks which pixels belong to an object, and it is mostly long runs of the same value.
- Do These Two Measurements Clash (Easy, free): A measurement on several qubits is written as a string of the letters I, X, Y and Z, one per qubit.
- Everyone Owes Everyone (Easy, free): Banks owe each other money all day long.
- Hiding in a Crowd of k (Easy, free): Removing names from a dataset does not make it anonymous.
- Outvote the Noise (Easy, free): A qubit is fragile, so one logical bit is spread across three physical ones.
- Two Sum (Easy, free): Given an array of numbers and a target, return the indices of the two numbers that add up to the target.
- Was This Written by a Model (Easy, free): One way to watermark a language model is to make it quietly prefer green tokens, where which tokens are green depends on the token before.
- Contains Duplicate (Easy): Given an array, return true if any value appears more than once, or false if every value is different.
- First Unique Character (Easy): Given a string, return the index of the first character that appears only once, or -1 if there isn't one.
- How Many Bag Slots? (Easy): A player picks up items[i] in amounts counts[i].
- Majority Element (Easy): Given an array where one value appears more than half the time, return that value.
- Model Accuracy (Easy): Given predictions and true labels, return the fraction the model got right, rounded to 3 decimals.
- Recently Used (Easy): Given the cache's keys in order of use, oldest first, and a key that was just used, return the new order.
- Valid Anagram (Easy): Given two strings s and t, return true if t uses exactly the same letters as s, the same number of times.
- Verse/Chorus Ratio (Easy): Given a song's section labels, like "verse" and "chorus", return [number of choruses, total sections].
- Counting Without a Slot per Item (Medium, free): To count how often each item appears in a huge stream, a hash map needs an entry per item.
- Name That Chord (Medium, free): Three notes played together form a triad, and its sound depends on the gaps between them.
- Neighbors on the Same Line (Medium, free): Each thread has its own counter, and they share nothing, yet the program crawls.
- Ranked the Right Way Round (Medium, free): AUC sounds like calculus and is really a count.
- Similar Sets, Tiny Fingerprints (Medium, free): Comparing two huge sets item by item is slow.
- Who Came Back? (Medium, free): Total active users can rise while a product quietly loses everyone who tries it.
- Whoever Is Left Standing (Medium, free): You want the items that make up more than one k-th of an endless stream, and there is no room to count everything.
- Alliteration Count (Medium): Given a list of words, return how many words start with the same letter as the word right before them, ignoring case.
- Bag-of-Words Cosine (Medium): Given two lists of words, return their bag-of-words cosine similarity, rounded to 3 decimals.
- Bigram Counts (Medium): Given a list of words, return a map from each pair of neighboring words to how many times that pair appears.
- Conflict Check (Medium): Given two lists of names, return true if any name shows up in both.
- Confusion Matrix (Medium): Given predictions and true labels, both 0 or 1, return [truePositives, falsePositives, falseNegatives, trueNegatives].
- Count Accidentals (Medium): Given a list of MIDI notes, return how many land on black piano keys.
- Count Rhyming Pairs (Medium): Given the last word of each line, return how many pairs of lines you can make that rhyme, where rhyming means the same last two letters.
- Dedupe Email List (Medium): Given a list of email addresses, remove the duplicates and keep the rest in their original order.
- Gini Impurity (Medium): Given a list of labels, return the Gini impurity, rounded to 3 decimals.
- Hold It Until It Fits (Medium): Given packet numbers in the order they arrive, and the number you expect first, return how many packets you can deliver in order by the end.
- Information Entropy (Medium): Given a list of labels, return the Shannon entropy in bits, rounded to 3 decimals.
- Isomorphic Strings (Medium): Given two strings, return true if you can turn one into the other by swapping each letter for another letter, the same way every time.
- Jaccard Similarity (Medium): Given two lists, return their Jaccard similarity, rounded to 3 decimals: how many values they share, divided by how many different values there are in total.
- Only Check the Neighbors (Medium): Checking every pair of objects for collisions is O(n squared).
- Precision (Medium): Given predictions and true labels, both 0 or 1, return the precision, rounded to 3 decimals.
- Probably Yes, Definitely No (Medium): Given a Bloom filter's size, the bit positions each added key set, and the positions of a key you're checking, return true if the filter says it might be there.
- Recall (Medium): Given predictions and true labels, both 0 or 1, return the recall, rounded to 3 decimals.
- Repeat Customers (Medium): Given a list of orders, each naming a customer, return how many customers ordered more than once.
- Replay What Was Committed (Medium): Given a write-ahead log as [transaction, kind] entries, where kind 0 is a write and 1 is a commit, return how many writes should be replayed after a crash.
- Rhyme Scheme (Medium): Given the last word of each line, return the rhyme scheme as letters.
- The Same Thing Twice (Medium): Given a list of build inputs like the source and the compiler flags, return one 32-bit cache key.
- Verdict Reached? (Medium): Given jury votes, 1 for guilty and 0 for not guilty, return "guilty" if more than half voted guilty, "acquit" if more than half voted not guilty, and "hung" otherwise.
- Was It Worth It? (Medium): Given a list of lookups and a cache size, count how many lookups were hits.
- Kick Out the Occupant (Hard, free): Cuckoo hashing gives every key two homes, one in each of two tables, so a lookup checks two slots and is done.
- Only One Choice Left (Hard, free): A formula is a list of clauses, and each clause is a list of literals: 3 means variable 3 is true, -3 means it is false.
- BPE Merge Step (Hard): Given a list of tokens, return the neighboring pair that shows up most often, as [left, right].
- Cross-Matter Conflict (Hard): Given a list of matters, each with a list of party names, return true if any party shows up in two different matters.
- Find the Chorus (Hard): Given a song's lines, return the line that shows up most often.
- k-Nearest Neighbors (Hard): Given labeled points, a query point, and k, return the label the k closest points vote for.
Practice another pattern
Or browse by subject
Browse all 676 puzzles