What can go wrong when looking up a key in a dictionary, and what is the worst-case time complexity?
AI answer
In the worst case, a lookup by key in a dict takes O(n) if many keys have the same hash and many collisions need to be checked. A lookup usually takes O(1). If the key is absent, accessing it with d[key] raises KeyError; use key in d to check for its presence, or d.get(key) to retrieve the value with a fallback. The key must also be hashable: for example, trying to use a list will raise TypeError: unhashable type.
Also asked at СБЕР
- Do you have any questions about the company and the role?
- Tell us about your experience: on which projects you worked at [company] and [company], what technologies you used, and what you were responsible for?
- Tell us about yourself: how did you come into development, what teams and products have you worked on, what functionality did you perform?
- What databases have you had to work with as a developer?
- Tell me about the product/project you worked on and your main tasks.
Similar questions
- What is the time complexity of searching a Python dictionary (`dict`), and what can be used as a dictionary key?
- What other data structures, besides hash tables, can be used to implement a map (dictionary) interface, and what would their time complexity be?
- What is the time complexity of a search?
- What is the complexity of inserting N identical strings of length k into an empty Python set if the hash function is overridden to be an O(1) constant?
- Are there situations where the complexity of operations with a dictionary will be worse than O(1)?