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

  1. 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.
  2. 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.
  3. 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.
  4. Everyone Owes Everyone (Easy, free): Banks owe each other money all day long.
  5. Hiding in a Crowd of k (Easy, free): Removing names from a dataset does not make it anonymous.
  6. Outvote the Noise (Easy, free): A qubit is fragile, so one logical bit is spread across three physical ones.
  7. 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.
  8. 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.
  9. Contains Duplicate (Easy): Given an array, return true if any value appears more than once, or false if every value is different.
  10. 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.
  11. How Many Bag Slots? (Easy): A player picks up items[i] in amounts counts[i].
  12. Majority Element (Easy): Given an array where one value appears more than half the time, return that value.
  13. Model Accuracy (Easy): Given predictions and true labels, return the fraction the model got right, rounded to 3 decimals.
  14. 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.
  15. 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.
  16. Verse/Chorus Ratio (Easy): Given a song's section labels, like "verse" and "chorus", return [number of choruses, total sections].
  17. 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.
  18. Name That Chord (Medium, free): Three notes played together form a triad, and its sound depends on the gaps between them.
  19. Neighbors on the Same Line (Medium, free): Each thread has its own counter, and they share nothing, yet the program crawls.
  20. Ranked the Right Way Round (Medium, free): AUC sounds like calculus and is really a count.
  21. Similar Sets, Tiny Fingerprints (Medium, free): Comparing two huge sets item by item is slow.
  22. Who Came Back? (Medium, free): Total active users can rise while a product quietly loses everyone who tries it.
  23. 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.
  24. 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.
  25. Bag-of-Words Cosine (Medium): Given two lists of words, return their bag-of-words cosine similarity, rounded to 3 decimals.
  26. Bigram Counts (Medium): Given a list of words, return a map from each pair of neighboring words to how many times that pair appears.
  27. Conflict Check (Medium): Given two lists of names, return true if any name shows up in both.
  28. Confusion Matrix (Medium): Given predictions and true labels, both 0 or 1, return [truePositives, falsePositives, falseNegatives, trueNegatives].
  29. Count Accidentals (Medium): Given a list of MIDI notes, return how many land on black piano keys.
  30. 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.
  31. Dedupe Email List (Medium): Given a list of email addresses, remove the duplicates and keep the rest in their original order.
  32. Gini Impurity (Medium): Given a list of labels, return the Gini impurity, rounded to 3 decimals.
  33. 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.
  34. Information Entropy (Medium): Given a list of labels, return the Shannon entropy in bits, rounded to 3 decimals.
  35. 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.
  36. 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.
  37. Only Check the Neighbors (Medium): Checking every pair of objects for collisions is O(n squared).
  38. Precision (Medium): Given predictions and true labels, both 0 or 1, return the precision, rounded to 3 decimals.
  39. 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.
  40. Recall (Medium): Given predictions and true labels, both 0 or 1, return the recall, rounded to 3 decimals.
  41. Repeat Customers (Medium): Given a list of orders, each naming a customer, return how many customers ordered more than once.
  42. 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.
  43. Rhyme Scheme (Medium): Given the last word of each line, return the rhyme scheme as letters.
  44. The Same Thing Twice (Medium): Given a list of build inputs like the source and the compiler flags, return one 32-bit cache key.
  45. 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.
  46. Was It Worth It? (Medium): Given a list of lookups and a cache size, count how many lookups were hits.
  47. 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.
  48. 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.
  49. BPE Merge Step (Hard): Given a list of tokens, return the neighboring pair that shows up most often, as [left, right].
  50. 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.
  51. Find the Chorus (Hard): Given a song's lines, return the line that shows up most often.
  52. 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