Hash Table
A hash table maps keys to bucket locations using a hash function. It aims for very fast average lookup, insertion and deletion.
Beginne mit diesem mentalen Modell
A hash table maps keys to bucket locations using a hash function. It aims for very fast average lookup, insertion and deletion.
So funktioniert es – ohne unnötigen Jargon
The hash function converts a key into an index-like value.
Different keys may collide, so the implementation needs a collision strategy such as chaining or open addressing.
Lies den Algorithmus als Folge von Entscheidungen
Hash the key.
Map the hash to a bucket.
Resolve collisions and compare actual keys.
A user id can be hashed to a bucket; the bucket then stores or finds the actual user entry.
class HashTable {
constructor(size = 16) {
this.buckets = Array.from({ length: size }, () => []);
}
hash(key) {
let h = 0;
for (const ch of String(key)) h = (h * 31 + ch.charCodeAt(0)) >>> 0;
return h % this.buckets.length;
}
}Lerne die Syntax nicht auswendig. Folge dem Zustand, der sich in jeder Zeile ändert, und verbinde ihn mit der oben beschriebenen Invariante.
avg O(1), worst O(n)
Do not quote this complexity mechanically. Ask which operation or input dimension it describes, then derive the cost from the loops, recursion depth, data-structure operations and any preprocessing assumptions.
Gut geeignet
- Choose it when its dominant operations match your workload.
- Compare memory overhead as well as Big-O.
Was typischerweise schiefgeht
- Calling average O(1) a guaranteed worst-case O(1).
- Ignoring key equality and collision handling.
Struktur einer professionellen Antwort
Explain average O(1), collisions, load factor and resizing. Those details distinguish a strong answer from 'it is just a key-value map'.
What problem does Hash Table solve?
A hash table maps keys to bucket locations using a hash function. It aims for very fast average lookup, insertion and deletion.
Was sollte ich zur Komplexität erwähnen?
Its typical cost here is avg O(1), worst O(n), but a complete answer should connect that cost to the representation, input constraints and the dominant operations.
Was ist der häufigste Fehler?
Calling average O(1) a guaranteed worst-case O(1).