foundation

Hash tables, maps, and sets

Average constant-time lookup, collision risk, key identity, and frequency counting patterns.

Hash tables power maps and sets by turning keys into buckets. Average lookup, insert, and delete are constant time, but memory grows with stored keys and pathological collision cases can degrade behavior. In JavaScript, Map preserves key identity more predictably than plain object dictionaries.

On interviews: Common tasks include two-sum complements, frequency counts, grouping anagrams, first seen indexes, deduplication, and visited sets for traversal.

Common pitfalls: Do not ignore memory cost. Be explicit about equality semantics for object keys. For ordered output, remember that hashing solves lookup, not necessarily sorted order.

Checklist:

  • Choose Map for arbitrary keys.
  • Count memory as O(k).
  • Explain collision assumptions.
  • Separate lookup from ordering.