Hash Table
A hash table maps keys to bucket locations using a hash function. It aims for very fast average lookup, insertion and deletion.
이 멘탈 모델부터 시작하세요
A hash table maps keys to bucket locations using a hash function. It aims for very fast average lookup, insertion and deletion.
불필요한 전문용어 없이 동작 방식 이해하기
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.
알고리즘을 연속된 의사결정으로 읽기
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;
}
}문법을 외우지 마세요. 각 줄에서 바뀌는 상태를 따라가고 위에서 설명한 불변식과 연결하세요.
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.
적합한 경우
- Choose it when its dominant operations match your workload.
- Compare memory overhead as well as Big-O.
보통 어디에서 문제가 생기는가
- Calling average O(1) a guaranteed worst-case O(1).
- Ignoring key equality and collision handling.
전문적인 답변 구조
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.
복잡도에 대해 무엇을 언급해야 하나요?
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.
가장 흔한 실수는 무엇인가요?
Calling average O(1) a guaranteed worst-case O(1).