NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior0 viewsAsked at 2 companies

What can go wrong when looking up a key in a dictionary, and what is the worst-case time complexity?

Practice

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.