
Understand it
Lookup cost depends on collisions, and collisions depend on the hash function. Rust's standard map uses randomized seeding for collision resistance, while the exact hash algorithm is not a stable public guarantee. Swapping in a faster non-cryptographic hasher is correct for internal keys and dangerous for user input.
A hash map maps keys to buckets using a hash function and resolves collisions according to its implementation. Expected lookup can be close to constant time under suitable distribution and load, but a worst-case collision pattern can degrade performance. In Rust, the standard map uses randomized seeding to make attacker-controlled collision selection harder; do not rely on one fixed hash algorithm as a permanent API guarantee. Choosing a custom faster hasher may help trusted internal keys but can weaken resistance to adversarial inputs.
The choice of key type matters too. A large string costs time to hash and compare. A small integer key is cheaper, but it may not represent the domain meaning as clearly. Hash maps do not promise a stable iteration order; do not serialize their iteration directly when output order matters. Capacity and growth affect allocation and memory overhead. A B-tree can be preferable when ordered traversal or range queries are central.
For an interview, separate algorithmic expectations from a security claim. State the key distribution, who controls keys, the required lookup rate, and whether order matters. Then explain how you would benchmark a realistic workload and test collision-sensitive behavior. The strong answer does not promise O(1) in every case or say the standard hasher is universally fastest; it connects a hasher choice to the input threat model.
Also discuss deletion: removing entries can leave internal capacity allocated, so a map that once held a million keys may retain memory after it becomes small. Rebuilding or shrinking can help when memory pressure matters. The API and version determine the exact behavior; measure the live process rather than assuming removal releases every byte.
Interview follow-up: How do entry limits and input-size limits complement collision resistance? Compare trusted integer IDs with public untrusted strings.
Read the code
use std::collections::HashMap;
fn main() {
let mut counts = HashMap::new();
for key in ["accept", "accept", "content-type"] {
*counts.entry(key).or_insert(0) += 1;
}
assert_eq!(counts.get("accept"), Some(&2));
}entry counts repeated keys. The seeded default hasher’s algorithm is not an API promise.
Practice starter
// An HTTP service accepts arbitrary client-provided strings as keys.
// A predictable fast hasher replaces the default HashMap hasher.Try it
Decide whether a faster hasher is safe for two different key sources.
What success looks like: The choice is justified by where the keys come from.
Check your understanding
Why review a fast non-cryptographic hasher for attacker-controlled string keys?
Reveal the answer
- HashMap rejects String keys whenever its hasher is customized.
- Adversarial collision patterns can worsen lookup cost and create denial-of-service risk.
- Choosing a faster hasher guarantees constant-time lookup for every input.
Consider the specific hasher, threat model and quotas. Default hashing policy is not a permanent promise of one named algorithm.
Go deeper
Common misconception: Choosing a faster hasher guarantees constant-time lookup for every input.
A faster hash only makes each hash cheaper. Lookup cost still depends on collisions, and a predictable hasher can let an attacker create them.
- Identify the contract: A hash map is O(1) until an adversary picks your keys.
- Trace the concrete example:
entrycounts repeated keys. The seeded default hasher’s algorithm is not an API promise. - Test the changed constraint: How do entry limits and input-size limits complement collision resistance? Compare trusted integer IDs with public untrusted strings.
Transfer challenge: How do entry limits and input-size limits complement collision resistance? Compare trusted integer IDs with public untrusted strings.
Connections
- Complexity in Practice: Constants, cache, and amortised cost
- Choosing a Test: Unit, property, fuzz, integration
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Hashing and Collisions (courseId "interview", lessonId "q-hashmap") Link: https://crabrix.com/learn/interview/q-hashmap/ Goal: Hash choice is a security decision whenever the keys are attacker-controlled. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-hashmap) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.