Engineering Craft · 16 min · free with an account

Hashing and Collisions

Lesson preview · about 30%

A hash map is O(1) until an adversary picks your keys.

Core idea

Hash choice is a security decision whenever the keys are attacker-controlled.

Start with the idea

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.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Interview Prep