• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

We need a deterministic way to convert arbitrary-sized keys (strings, numbers) into fixed-size array indices for O(1) lookup.

Core Idea

A hash function maps keys to integer values (array indices) such that equal keys always produce the same hash value, and the distribution is as uniform as possible.

How It Works

  1. Take input key (string, number, object)
  2. Apply mathematical transformation (e.g., modulo, multiplication, bit shifting)
  3. Output is an integer in range [0, table_size-1]
  4. Good hash functions minimize collisions
hash_fn cluster_output Hash Values Input Input Keys 'Alice', 42, 'Bob' Function Hash Function h(key) Input->Function H1 h('Alice') = 3 Function->H1 H2 h(42) = 7 Function->H2 H3 h('Bob') = 3 ← collision! Function->H3

Key Properties

  • Deterministic: same key → same hash always
  • Uniform distribution minimizes collisions
  • Fast to compute (shouldn’t be slower than the data structure it serves)
  • Examples: division method, multiplication method, universal hashing

Connections

Edge Cases & Gotchas

  • Poor hash functions cause clustering (many collisions)
  • String hashing must handle variable lengths
  • Cryptographic hash functions are overkill for hash tables (too slow)
  • Changing hash function requires rehashing entire table