Junior — Middle
Explain what a hash table data structure is and how it works.
sobes.tech AI
Answer from AI
A hash table is a data structure that stores key-value pairs and provides very fast access to data by key. It works using a hash function that transforms the key into an array index where the value is stored.
Working principle:
- When adding an element, the key is passed through the hash function, which returns an index.
- The value is stored in the array at this index.
- When searching for a key, the hash function again calculates the index, and the value is quickly found at that index.
If two keys produce the same index (collision), a collision resolution method is used, such as chaining (linked lists) or open addressing.
This allows insertion, search, and deletion operations to be performed on average in O(1) time.