Junior — Middle
What types of data structures would you use to ensure the fastest possible information retrieval?
sobes.tech AI
Answer from AI
For the fastest information retrieval, data structures with quick access by key are usually used:
- Hash tables (dict in Python): provide an average search time of O(1), suitable for quick access by a unique key.
- Sets: for fast membership testing.
- Trees (e.g., balanced trees): if ordered search or range queries are needed.
Example of using dict for quick search:
users = {
'alice': {'age': 30},
'bob': {'age': 25}
}
# Quick search by name
user = users.get('alice')
if user:
print(user['age']) # 30