
The key takeaways
- Hash map time complexity is O(1) average, not O(1) guaranteed: uniform hashing and a controlled load factor are the two assumptions that make the math hold.
- Collision resolution splits into two families: separate chaining (linked lists or trees per bucket) and open addressing (probe sequences within the array itself).
- Resizing costs O(n) but amortizes to O(1) per insert: table doubling ensures each insertion pre-pays a constant amount of future rehash work; linear growth would not.
- Load factor thresholds differ across languages: CPython triggers at 2/3, Java at 0.75, Rust's SwissTable at ~7/8, each trading memory for collision probability.
- Hash flooding is a real DoS vector: adversarially chosen keys force O(n²) behavior; the defenses are randomized seeds (Python, Rust) and treeified collision chains (Java).
- The Two Sum pattern generalizes widely: any problem where you scan for a complement or "have I seen this" reduces from O(n²) to O(n) with a hash map.
- The clearest interview signal is a nested loop with an inner scan: that inner search is almost always a hash map replacement waiting to happen.
Write the nested loop solution to Two Sum. O(n²). Now rewrite it with a hash map. O(n). The performance gap is real, and hash map time complexity, specifically that "O(1) average-case lookup," deserves more scrutiny than most engineers give it.
That word "average" is doing suspicious amounts of work. Hash maps degrade silently under adversarial inputs, charge you a hidden O(n) during resizing, and behave differently in every language's standard library. Know the mechanics and you can use this structure with confidence. Skip them and you'll be explaining an O(n²) production incident to a very patient engineer at 3 AM.

Nobody asked for the full explanation. Victor delivered anyway.
A Bucket Array Is Not a Black Box
A hash map stores key-value pairs in an array of "buckets." To insert a key, you run it through a hash function that returns an integer, then take that integer modulo the array length to get a bucket index.
bucket_index = hash(key) % capacityFor a good hash function on diverse keys, each bucket gets roughly equal share of the load. That's where O(1) comes from. Not magic. Statistics. Which is to say: reliable on real-world inputs, and completely irrelevant when someone is deliberately sending you adversarial ones.

Two keys claiming bucket 5. The birthday paradox strikes again.
The hash function itself matters enormously. A good one distributes keys uniformly across all buckets, is deterministic (same key always gives the same hash), runs fast, and produces an avalanche effect where a small change in the input causes a large, unpredictable change in the output.
The hash function only needs to be consistent within a single process. Python's hash() randomizes its seed between interpreter restarts by default since Python 3.3, specifically to prevent hash flooding attacks. More on that in a moment.
Hash Map Collision Resolution: Two Strategies
No matter how good your hash function, two keys will eventually land in the same bucket. The pigeonhole principle guarantees it. Even with far fewer keys than buckets, the birthday paradox means the first collision arrives much sooner than intuition suggests. The birthday paradox is the universe's way of reminding you that probability is not your friend.
There are two main strategies.
Separate Chaining
Each bucket holds a pointer to a linked list (or dynamic array) of entries that hash to that index. On lookup, you hash to the bucket and scan the chain.

Lookup finds the bucket in O(1). Then it scans the chain. That part is the fine print.
Java's HashMap uses this approach. Since Java 8, once a chain grows beyond 8 elements, it converts the linked list to a red-black tree, turning worst-case chain traversal from O(n) to O(log n). The table must also have at least 64 buckets before treeification kicks in; below that threshold, Java resizes the whole table instead. Java built the fire escape that nobody ever uses in normal operation. But it's there, it's correct, and it's absolutely why the HashMap source code is 750 lines long.
Open Addressing
Every entry lives in the bucket array itself. No external pointers. On a collision, you probe the next available slot according to some sequence.

KEY_X had a rough commute. KEY_Y found parking immediately. This is what load factor determines.
The simplest probe sequence is linear probing: check slot i, then i+1, then i+2. It has excellent cache locality since you're scanning contiguous memory, but it causes primary clustering where filled slots accumulate into long runs that slow subsequent insertions.
Quadratic probing (check i, i+1, i+4, i+9, ...) reduces clustering but loses some cache locality and can fail to probe all slots unless the table size is a prime or a power of two.
CPython uses a pseudo-random perturbation scheme. The sequence is idx = (idx * 5 + 1 + perturb) % capacity where perturb starts as the full hash value, then right-shifts by 5 each step. Once perturb reaches zero, the sequence degenerates to a linear congruential generator that is mathematically guaranteed to visit every bucket exactly once (by the Hull-Dobell theorem) before repeating.
Robin Hood hashing is an open-addressing refinement worth knowing. When inserting a new key, if that key is farther from its ideal slot than the key currently occupying that slot, the two swap and insertion continues with the displaced key. This keeps probe lengths equalized across all entries, dramatically reducing variance. Lookups can also terminate early: if you probe past a slot whose key is closer to home than your key would be, your key cannot be in the table.
Rust's standard HashMap uses Google's SwissTable (via the hashbrown crate). It stores one-byte "control bytes" per slot separately from the actual data. A lookup extracts the low 7 bits of the hash as a fingerprint, then scans 16 control bytes at once using SIMD instructions. Most lookups find their answer in a single 128-bit SIMD comparison without ever touching the key-value data. This is roughly 2x faster than the previous standard library implementation and uses 1 byte of overhead per slot instead of 8.

One SIMD instruction checks 16 slots at once. Most lookups never touch the actual key-value data.
Core Operations
| Operation | Average Case | Worst Case | Space |
|---|---|---|---|
get(key) |
O(1) | O(n) | O(1) |
put(key, value) |
O(1) amortized | O(n) | O(1) amortized |
delete(key) |
O(1) | O(n) | O(1) |
contains(key) |
O(1) | O(n) | O(1) |
| Build from n pairs | O(n) | O(n²) | O(n) |
| Iterate all entries | O(n) | O(n) | O(1) |

The spikes look terrifying. The dashed line is what actually matters.
Why Hash Map Time Complexity Is O(1) on Average
With a load factor of α (entries / buckets), the expected number of probes for a lookup under uniform hashing is bounded by 1 + α/2 for separate chaining and 1 / (1 - α) for open addressing. When α is kept below some constant threshold, these quantities are O(1) constants.
At α = 0.75 (Java's default), open addressing expects about 4 probes per lookup. At α = 2/3 (CPython's threshold), about 3 probes. The table never fully fills; the load factor threshold is deliberately chosen to keep the expected probe count small.
The critical assumption is uniform hashing: each key is equally likely to map to any bucket, independent of other keys. Real hash functions approximate this well for diverse, real-world keys. They do not approximate it for adversarially chosen keys.
Why Insert Is O(1) Amortized, Not Just O(1)
Most insertions are O(1): hash, find slot, write. But when the table hits its load factor threshold, everything pauses for a resize. The whole table. Every key. Rehashed. Right now. The table doubles in size, and every existing entry rehashes into the new table. That single operation costs O(n).
The reason the amortized cost is still O(1) is that doubling is geometric growth. Here's the accounting argument:
Imagine a table with capacity C that just finished resizing. To trigger the next resize, you need to insert roughly C/2 more entries (pushing the load factor back to the threshold). The upcoming resize will cost O(C). Spread that cost across those C/2 insertions: each one effectively pre-pays 2 units of future resize work. The pre-payment is constant, so the amortized cost per insertion is constant.
The comparison with linear growth makes this concrete. Suppose instead of doubling you added a fixed 100 slots each resize. After the first resize you'd have 200 slots. After k resizes you'd have 100 + 100k slots and you'd have done 100 + 200 + 300 + ... + 100k = O(k²) total rehashing work for O(k) rounds. That's O(n) amortized per insert. The geometric growth is not optional. It's what makes the whole structure work.
CPython has one wrinkle: for dictionaries with 50,000 or fewer used keys, it resizes to used_count * 4 (quadrupling, not doubling). For larger dicts, it uses used_count * 2. The quadrupling for small dicts further amortizes cost when the dict is growing rapidly from a small base.
Load Factor Thresholds Across Real Implementations
Every major implementation picks a load factor and resizes when it's crossed. The choices differ more than you'd expect from teams all supposedly optimizing the same thing.
| Implementation | Load Factor Threshold | Collision Strategy |
|---|---|---|
CPython dict |
2/3 (~0.67) | Open addressing, pseudo-random probing |
Java HashMap |
0.75 | Separate chaining, treeify at chain ≥ 8 |
Rust HashMap |
~0.875 (7/8) | SwissTable, quadratic group probing + SIMD |
Go map (pre-1.24) |
~6.5 entries per 8-slot bucket | 8-slot buckets with overflow bucket chaining |
Go map (1.24+) |
Similar | SwissTable groups, splits at 128 groups |
A lower load factor reduces collisions but wastes memory; a higher one saves space but lengthens collision chains. Java's 0.75 is backed by the Poisson distribution: at that load, the probability of any single bucket having 8 or more entries (the treeification threshold) is approximately 0.00000006. The treeification path is theoretically correct but practically never taken under normal inputs. It's a six-sigma safety net for a six-sigma event.
CPython's compact dict design, introduced in Python 3.6, separates the hash table from the entries array. The hash table stores only small integer indices (1, 2, 4, or 8 bytes each depending on table size) that point into a densely-packed array of (hash, key, value) tuples stored in insertion order. This is why Python dicts preserve insertion order since 3.7, and why the memory layout is significantly more cache-friendly than storing full entries directly in the bucket array.

The indices array is tiny. The entries array is dense and cache-friendly. And iteration walks it in insertion order, which is why Python dicts are sorted by age of entry.
O(n) Is Real, and Attackers Know It
If every key you insert maps to the same bucket, every lookup scans the entire collision chain. That's O(n) per operation, and n insertions take O(n²) total.
In 2011, researchers demonstrated that you could take down a web server with a carefully crafted HTML form. No malware. No zero-day. Just math. By sending HTTP POST data with keys chosen to collide under the server's hash function, they could force the server to spend O(n²) time parsing what looked like ordinary form data. PHP, Ruby, Java, Python, and ASP.NET were all affected. It was a bad week for everyone who had ever typed O(1) without thinking too hard about it.
The defenses:
- Hash randomization. Seed the hash function with a random value at startup. Attackers can't predict collisions without knowing the seed. Python does this since 3.3; Rust uses SipHash-1-3 by default, which is designed to be collision-resistant even when the attacker knows the function.
- Treeification. Even if many keys collide, cap chain traversal at O(log n). Java's red-black tree conversion is exactly this defense.
- Better hash functions for untrusted input. SipHash and variants provide cryptographic-quality mixing at very low cost.
The practical takeaway: don't assume O(1) for hash maps that process untrusted external keys. In interview settings this comes up as "what's the worst case?" The right answer is O(n) per operation, O(n²) for n insertions, and the reason is adversarial key collisions. If an interviewer asks and you say "O(1), always," you've handed them the follow-up question on a platter.
One Structure, Every Language
# Python's built-in dict: open addressing with pseudo-random probing
# Preserves insertion order since Python 3.7 (CPython 3.6+)
phone_book: dict[str, str] = {}
# Put
phone_book["alice"] = "555-1234"
phone_book["bob"] = "555-5678"
# Get (raises KeyError if missing)
number = phone_book["alice"]
# Get with default
number = phone_book.get("charlie", "unknown")
# Delete
del phone_book["bob"]
# Contains
if "alice" in phone_book:
print("found")
# Iterate (guaranteed insertion order)
for name, number in phone_book.items():
print(f"{name}: {number}")
# Frequency count: Counter is a dict subclass
from collections import Counter, defaultdict
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
freq: dict[str, int] = Counter(words)
# Counter({'apple': 3, 'banana': 2, 'cherry': 1})
# defaultdict avoids KeyError on first access
graph: dict[str, list[str]] = defaultdict(list)
graph["a"].append("b") # no need to initialize graph["a"] first# Python 2 dict: similar internals but no insertion-order guarantee
phone_book = {}
# Put / Get / Delete / Contains
phone_book["alice"] = "555-1234"
number = phone_book.get("alice", "unknown")
del phone_book["alice"]
exists = "alice" in phone_book
# In Python 2, items() copies the list; prefer iteritems() for large dicts
for name, number in phone_book.iteritems():
print("{}: {}".format(name, number))
# viewkeys() / viewvalues() / viewitems() are set-like views (Python 2.7+)
for name in phone_book.viewkeys():
print(name)
# Frequency count
from collections import Counter, defaultdict
freq = Counter(["apple", "banana", "apple"])// Use Map, not plain objects, for proper hash map semantics.
// Plain objects coerce keys to strings and inherit prototype pollution risks.
const phoneBook = new Map();
// Put
phoneBook.set("alice", "555-1234");
phoneBook.set("bob", "555-5678");
// Get (returns undefined if missing)
const number = phoneBook.get("alice");
// Get with default
const safe = phoneBook.get("charlie") ?? "unknown";
// Contains
if (phoneBook.has("alice")) {
console.log("found");
}
// Delete
phoneBook.delete("bob");
// Size
console.log(phoneBook.size);
// Iterate (insertion order guaranteed by spec)
for (const [name, num] of phoneBook) {
console.log(`${name}: ${num}`);
}
// Frequency count
function countFrequencies(arr) {
const freq = new Map();
for (const item of arr) {
freq.set(item, (freq.get(item) ?? 0) + 1);
}
return freq;
}
// Set for membership-only use (no values needed)
const seen = new Set();
seen.add("alice");
console.log(seen.has("alice")); // true// Strongly-typed Map
const phoneBook = new Map<string, string>();
phoneBook.set("alice", "555-1234");
const number: string | undefined = phoneBook.get("alice");
const exists: boolean = phoneBook.has("alice");
phoneBook.delete("alice");
// Type-safe frequency counter (generic)
function countFrequencies<T>(items: T[]): Map<T, number> {
const freq = new Map<T, number>();
for (const item of items) {
freq.set(item, (freq.get(item) ?? 0) + 1);
}
return freq;
}
// Record<K, V> for string-keyed maps with a known, fixed shape
const config: Record<string, string> = {
host: "localhost",
port: "5432",
};
// ReadonlyMap for immutable references (prevents set/delete at compile time)
const constants: ReadonlyMap<string, number> = new Map([
["MAX_RETRIES", 3],
["TIMEOUT_MS", 5000],
]);import java.util.*;
// HashMap: O(1) average, not thread-safe.
// Use ConcurrentHashMap for concurrent access.
// Use LinkedHashMap for insertion-order iteration.
Map<String, String> phoneBook = new HashMap<>();
// Put
phoneBook.put("alice", "555-1234");
// Get (returns null if missing)
String number = phoneBook.get("alice");
// Get with default (Java 8+)
String safe = phoneBook.getOrDefault("charlie", "unknown");
// Contains
boolean exists = phoneBook.containsKey("alice");
// Delete
phoneBook.remove("alice");
// merge() is the cleanest frequency counter (Java 8+)
Map<String, Integer> freq = new HashMap<>();
String[] words = {"apple", "banana", "apple", "cherry"};
for (String word : words) {
freq.merge(word, 1, Integer::sum);
}
// computeIfAbsent() for adjacency lists and grouped maps
Map<String, List<String>> graph = new HashMap<>();
graph.computeIfAbsent("a", k -> new ArrayList<>()).add("b");
graph.computeIfAbsent("a", k -> new ArrayList<>()).add("c");
// Iterate (no order guarantee with HashMap)
for (Map.Entry<String, String> entry : phoneBook.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}#include <unordered_map>
#include <string>
#include <iostream>
#include <stdexcept>
std::unordered_map<std::string, std::string> phoneBook;
// Put
phoneBook["alice"] = "555-1234"; // inserts or updates
phoneBook.insert({"bob", "555-5678"}); // inserts only; no-op if exists
phoneBook.emplace("charlie", "555-9012"); // construct in-place
// CAUTION: operator[] inserts a default-constructed value on miss.
// Use at() to throw, or find() to avoid insertion.
std::string number = phoneBook.at("alice"); // throws std::out_of_range on miss
auto it = phoneBook.find("alice");
if (it != phoneBook.end()) {
std::cout << it->second << "\n";
}
// Contains (C++20)
bool exists = phoneBook.contains("alice");
// Delete
phoneBook.erase("alice");
// Frequency count: operator[] zero-initializes int on first access
std::unordered_map<std::string, int> freq;
std::vector<std::string> words = {"apple", "banana", "apple"};
for (const auto& word : words) {
freq[word]++;
}
// Pre-allocate to avoid rehashing during bulk inserts
phoneBook.reserve(1000);
phoneBook.max_load_factor(0.5f); // tune the threshold#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// C has no built-in hash map. Separate chaining with djb2 hashing.
#define TABLE_SIZE 64
typedef struct Node {
char *key;
char *value;
struct Node *next;
} Node;
typedef struct {
Node *buckets[TABLE_SIZE];
int size;
} HashMap;
static unsigned int hash(const char *key) {
unsigned int h = 5381;
int c;
while ((c = (unsigned char)*key++)) {
h = ((h << 5) + h) + c; // h * 33 + c (djb2)
}
return h % TABLE_SIZE;
}
void hashmap_put(HashMap *map, const char *key, const char *value) {
unsigned int idx = hash(key);
for (Node *node = map->buckets[idx]; node != NULL; node = node->next) {
if (strcmp(node->key, key) == 0) {
free(node->value);
node->value = strdup(value);
return;
}
}
Node *node = malloc(sizeof(Node));
node->key = strdup(key);
node->value = strdup(value);
node->next = map->buckets[idx];
map->buckets[idx] = node;
map->size++;
}
const char *hashmap_get(const HashMap *map, const char *key) {
unsigned int idx = hash(key);
for (Node *node = map->buckets[idx]; node != NULL; node = node->next) {
if (strcmp(node->key, key) == 0) return node->value;
}
return NULL;
}
void hashmap_delete(HashMap *map, const char *key) {
unsigned int idx = hash(key);
Node **prev = &map->buckets[idx];
for (Node *node = *prev; node != NULL; prev = &node->next, node = *prev) {
if (strcmp(node->key, key) == 0) {
*prev = node->next;
free(node->key);
free(node->value);
free(node);
map->size--;
return;
}
}
}package main
import "fmt"
func main() {
// Go's built-in map (pre-1.24: 8-slot buckets with overflow chaining;
// 1.24+: SwissTable groups). Load factor ~6.5 entries per bucket.
phoneBook := make(map[string]string)
// Provide a capacity hint to reduce early resizes (not a size limit)
phoneBook = make(map[string]string, 100)
// Put
phoneBook["alice"] = "555-1234"
phoneBook["bob"] = "555-5678"
// Get: returns zero value on miss (empty string here)
number := phoneBook["alice"]
_ = number
// Get with existence check (the comma-ok idiom)
if num, ok := phoneBook["charlie"]; ok {
fmt.Println(num)
}
// Delete
delete(phoneBook, "alice")
// Iterate (random order; maps are deliberately unordered in Go)
for name, num := range phoneBook {
fmt.Printf("%s: %s\n", name, num)
}
// Frequency count: zero value of int is 0, so increment works immediately
words := []string{"apple", "banana", "apple", "cherry"}
freq := make(map[string]int)
for _, word := range words {
freq[word]++
}
fmt.Println(freq)
}use std::collections::HashMap;
fn main() {
// std::collections::HashMap uses SwissTable (hashbrown) backed by SipHash-1-3.
// For performance-critical code, swap to AHash via the ahash crate.
let mut phone_book: HashMap<String, String> = HashMap::new();
// Put
phone_book.insert("alice".to_string(), "555-1234".to_string());
phone_book.insert("bob".to_string(), "555-5678".to_string());
// Get: returns Option<&V>
match phone_book.get("alice") {
Some(number) => println!("{number}"),
None => println!("not found"),
}
// Contains
let exists = phone_book.contains_key("alice");
// Delete: returns Option<V> with the removed value
let removed = phone_book.remove("alice");
// Entry API: the cleanest way to do "insert or update"
// Avoids the double-lookup that .contains_key() + .insert() would do
let mut freq: HashMap<&str, u32> = HashMap::new();
let words = vec!["apple", "banana", "apple", "cherry"];
for word in &words {
*freq.entry(word).or_insert(0) += 1;
}
// Iterate
for (name, number) in &phone_book {
println!("{name}: {number}");
}
// Pre-allocate
let mut large: HashMap<i32, i32> = HashMap::with_capacity(1000);
let _ = (exists, removed, large.len());
}// Swift's Dictionary uses open addressing
var phoneBook: [String: String] = [:]
// Put
phoneBook["alice"] = "555-1234"
phoneBook["bob"] = "555-5678"
// Get: returns Optional<Value>
let number = phoneBook["alice"] // String?
// Get with inline default (Swift 4+)
let safe = phoneBook["charlie", default: "unknown"]
// Contains
let exists = phoneBook["alice"] != nil
// Delete
phoneBook.removeValue(forKey: "alice")
phoneBook["bob"] = nil // also removes the key
// Iterate (no order guarantee)
for (name, number) in phoneBook {
print("\(name): \(number)")
}
// Frequency count with subscript default
let words = ["apple", "banana", "apple", "cherry"]
var freq: [String: Int] = [:]
for word in words {
freq[word, default: 0] += 1
}
// Merge two dictionaries, resolving conflicts with a closure
var a = ["x": 1, "y": 2]
let b = ["y": 3, "z": 4]
a.merge(b) { _, new in new } // keep the incoming value on conflict// Kotlin distinguishes read-only and mutable map types at compile time
val readOnly: Map<String, String> = mapOf("alice" to "555-1234")
val mutable: MutableMap<String, String> = mutableMapOf()
// Put
mutable["alice"] = "555-1234"
mutable.put("bob", "555-5678")
// Get: returns null on miss
val number: String? = mutable["alice"]
// Get with default
val safe = mutable.getOrDefault("charlie", "unknown")
val safe2 = mutable.getOrElse("charlie") { "computed-default" }
// Contains
val exists = "alice" in mutable
// Delete
mutable.remove("alice")
// Iterate (insertion order preserved by LinkedHashMap, which is the default)
for ((name, num) in mutable) {
println("$name: $num")
}
// Manual frequency count
val words = listOf("apple", "banana", "apple", "cherry")
val freq = mutableMapOf<String, Int>()
for (word in words) {
freq[word] = freq.getOrDefault(word, 0) + 1
}
// Idiomatic Kotlin: groupingBy().eachCount()
val freqIdiomatic: Map<String, Int> = words.groupingBy { it }.eachCount()
println(freqIdiomatic) // {apple=2, banana=1, cherry=1}using System;
using System.Collections.Generic;
using System.Linq;
// Dictionary<TKey, TValue>: open addressing with prime-size table
var phoneBook = new Dictionary<string, string>();
// Put
phoneBook["alice"] = "555-1234"; // insert or update
phoneBook.Add("bob", "555-5678"); // throws ArgumentException if key exists
// Get: throws KeyNotFoundException on miss
string number = phoneBook["alice"];
// Safe get with TryGetValue (avoids double-lookup vs ContainsKey + indexer)
if (phoneBook.TryGetValue("charlie", out string? found)) {
Console.WriteLine(found);
}
// Get with default (.NET 3.0+)
string safe = phoneBook.GetValueOrDefault("charlie", "unknown");
// Contains
bool exists = phoneBook.ContainsKey("alice");
// Delete: returns bool indicating whether the key was present
bool removed = phoneBook.Remove("alice");
// Iterate
foreach (var (name, num) in phoneBook) {
Console.WriteLine($"{name}: {num}");
}
// Frequency count via LINQ
string[] words = { "apple", "banana", "apple", "cherry" };
Dictionary<string, int> freq = words
.GroupBy(w => w)
.ToDictionary(g => g.Key, g => g.Count());
// Or manually
var freq2 = new Dictionary<string, int>();
foreach (var word in words) {
freq2[word] = freq2.GetValueOrDefault(word) + 1;
}# Ruby Hash: open addressing with Robin Hood hashing (MRI >= 2.4)
# Preserves insertion order since Ruby 1.9
phone_book = {}
# Put
phone_book["alice"] = "555-1234"
phone_book.store("bob", "555-5678")
# Get: returns nil on miss
number = phone_book["alice"]
# Get with default
safe = phone_book.fetch("charlie", "unknown")
safe2 = phone_book.fetch("charlie") { "computed-default" }
# Contains
exists = phone_book.key?("alice")
# Delete
phone_book.delete("alice")
# Iterate (insertion order preserved)
phone_book.each do |name, number|
puts "#{name}: #{number}"
end
# tally (Ruby 2.7+): idiomatic frequency count
words = %w[apple banana apple cherry banana apple]
freq = words.tally
# {"apple" => 3, "banana" => 2, "cherry" => 1}
# each_with_object for older Ruby
freq2 = words.each_with_object(Hash.new(0)) { |w, h| h[w] += 1 }
# group_by: groups elements by the block's return value
groups = words.group_by { |w| w }
# {"apple" => ["apple", "apple", "apple"], ...}<?php
// PHP arrays are ordered hash maps: a doubly-linked list threaded through a
// hash table. Preserves insertion order. Keys can be integers or strings.
$phoneBook = [];
// Put
$phoneBook["alice"] = "555-1234";
$phoneBook["bob"] = "555-5678";
// Get: PHP emits a notice (E_NOTICE) if the key is missing, returns null
$number = $phoneBook["alice"];
// Safe get with null coalescing (PHP 7+)
$safe = $phoneBook["charlie"] ?? "unknown";
// Contains
$exists = array_key_exists("alice", $phoneBook); // works for null values
$exists2 = isset($phoneBook["alice"]); // false when value is null
// Delete
unset($phoneBook["alice"]);
// Iterate (insertion order preserved)
foreach ($phoneBook as $name => $number) {
echo "{$name}: {$number}\n";
}
// Frequency count
$words = ["apple", "banana", "apple", "cherry"];
$freq = array_count_values($words);
// ["apple" => 2, "banana" => 1, "cherry" => 1]
// Manually (for non-string/int values where array_count_values won't work)
$freq2 = [];
foreach ($words as $word) {
$freq2[$word] = ($freq2[$word] ?? 0) + 1;
}The Classes of Problem This Unlocks
A hash map is the right tool whenever the bottleneck is retrieval by key. The recurring patterns:
Eliminating redundant scanning. The naive solution scans the entire collection for each query. Moving those queries into a hash map drops the inner loop entirely. Two Sum is the canonical example, but the pattern appears everywhere.
Counting and grouping. Frequency tables, histograms, grouping items by a derived key. The derived key is often the trick: sorted characters for anagram detection, prefix sums for subarray problems, a tuple of coordinates for grid compression.
Memoization. Dynamic programming is "don't recompute what you've already computed." A hash map is the natural store for that cache when the state space is irregular or sparse. For dense, integer-indexed states, a plain array is faster.
Bidirectional lookups. When you need both forward and reverse lookups in O(1), two hash maps beat any structure trying to serve both directions at once. Common in problems about pairing or inverse mapping.
Deduplication and membership testing. Any "have I seen this before?" query with no ordering requirement. Use a hash set, which is the same structure with values stripped.
Graph adjacency lists. When node identifiers are not small integers, or the graph is sparse, Map<Node, List<Node>> is more memory-efficient than a 2D adjacency matrix.
Spotting the Signal
The clearest signal is a nested loop with an inner scan. Any time you see "for each element, search the rest of the collection for X," that inner search is a candidate for hash map replacement.
The "have I seen this before?" pattern is subtler but just as common. Problems like Contains Duplicate, Valid Anagram, LRU Cache, and Longest Substring Without Repeating Characters all follow the same shape: scan left to right, record what you've seen, query that record for each new element.
Pair and complement problems are the third category. If the relationship between two elements can be expressed as f(a) == g(b) for some functions f and g, precompute f(a) values into a hash map and look up g(b) in O(1) as you scan. Two Sum is the archetype.
Worked Example 1: Two Sum
Problem. Given an array of integers and a target sum, return the indices of two numbers that add up to the target.
Why the brute force is slow. For each number at index i, you scan every index j > i looking for target - nums[i]. Two nested loops. O(n²).
The hash map insight. While scanning left to right, you want to know "does the complement of the current number exist somewhere earlier in the array?" A hash map answers that in O(1). Store each number and its index as you process it. For each new number, check if its complement is already in the map.
def two_sum(nums: list[int], target: int) -> list[int]:
seen: dict[int, int] = {} # value -> index
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []The inner scan disappears entirely. O(n) time, O(n) space. This is the exact trade-off hash maps make: you pay in memory to eliminate the time cost of repeated searching.
Worked Example 2: Subarray Sum Equals K
Problem. Given an integer array and a target k, count the number of contiguous subarrays whose elements sum to k.
This one is less obvious. The key identity is: sum(i..j) == k if and only if prefix[j] - prefix[i-1] == k, which rearranges to prefix[i-1] == prefix[j] - k.
As you scan and accumulate a running prefix sum, you ask: "how many times have I seen the value running_sum - k at an earlier index?" A hash map of seen prefix sums answers that in O(1) per step.
def subarray_sum(nums: list[int], k: int) -> int:
prefix_counts: dict[int, int] = {0: 1} # the empty prefix has sum 0
running_sum = 0
count = 0
for num in nums:
running_sum += num
count += prefix_counts.get(running_sum - k, 0)
prefix_counts[running_sum] = prefix_counts.get(running_sum, 0) + 1
return countThe {0: 1} initialization is not optional. Without it, subarrays starting at index 0 that sum to k are missed, because prefix[j] - prefix[-1] == k requires a count for prefix sum 0. That single line is a common interview bug. It's also a great way to tell who has actually run the code and who just memorized the pattern.
Both of these problems look like O(n²) at first glance. Both become O(n) the moment you reach for a hash map. That's the pattern worth internalizing.
Recap
- A hash map stores key-value pairs in a bucket array, using a hash function to map keys to indices.
- Collision resolution is either separate chaining (linked lists or trees per bucket) or open addressing (probe sequence within the array itself).
- O(1) average case depends on uniform hashing and a controlled load factor. It is not guaranteed.
- Resizing must double the table (geometric growth). Linear growth gives O(n) amortized insert; doubling gives O(1) amortized.
- Worst case is O(n) per operation when many keys collide. Hash flooding is a real denial-of-service attack vector. The defenses are randomized hash seeds and treeified chains.
- Language implementations differ significantly. CPython uses compact open addressing with insertion-order guarantees. Java uses chaining with a red-black tree fallback for long chains. Rust uses SwissTable with SIMD for parallel slot scanning. Go uses 8-slot buckets with overflow chaining (pre-1.24) or SwissTable groups (1.24+).
- The signals to reach for a hash map: nested loop with inner scan, "have I seen this before?" queries, complement or pairing problems.
If you want to practice converting that nested-loop instinct into hash map solutions under interview conditions, SpaceComplexity runs voice-based mock interviews with rubric-based feedback on exactly this kind of pattern recognition. Working through "why a hash map here?" out loud with an interviewer watching is where it actually sticks.
For the sliding window, which combines a hash map with two pointers to handle substring and subarray problems, see Nested Loops Will Cost You the Offer. For memoization, where a hash map serves as the cache in recursive dynamic programming, see Dynamic Programming Is Just Recursion With a Memory.
Further Reading
- Hash table (Wikipedia). Comprehensive coverage of all collision strategies, load factor theory, and hash function requirements.
- Python
__hash__documentation. Official docs on Python's hashing protocol, seed randomization, and what makes a type hashable. - Java HashMap Javadoc. Official docs, including the Poisson distribution reasoning behind the 0.75 load factor threshold.
- Rust std::collections::HashMap. Documents the SwissTable backing, SipHash default, and entry API.
- Hash table cheat sheet (Tech Interview Handbook). Quick reference for common interview problem patterns and time complexities.
- GeeksforGeeks: Hashing Data Structure. Worked implementations and problem walkthroughs across collision strategies.
