Junior — Middle
Explain the principle of open addressing in the implementation of associative containers like map.
sobes.tech AI
Answer from AI
Open addressing is a collision resolution method in hash tables, such as maps. When adding a key-value pair, if the computed hash points to an occupied cell, searching for a free cell occurs within the array following a certain sequence (e.g., linear or quadratic probing).
Instead of using linked lists for collisions (chains), open addressing stores all elements within a single array, checking neighboring positions sequentially until an empty slot is found.
Example of linear probing:
// hash - hash function
// table - array for storing elements
// i - attempt number
index := (hash(key) + i) % len(table)
This approach saves memory on pointers but requires control of the table's load to avoid performance degradation.