What is the time complexity of python dict has_key() method

前端 未结 1 1021
情深已故
情深已故 2021-01-22 22:24

What is the time complexity of python dict has_key() method is it O(1) as in case of key in dict.

1条回答
  •  不思量自难忘°
    2021-01-22 23:18

    Short answer: worst case it is O(n). But the average case time complexity is O(1). The worst case is however very rare.

    When you do a lookup, the key is first double hashed (once by the type of the key and once by the dictionary). Based on that result, we know in which bucket we have to search, and we start searching.

    It is however possible that hash collissions occur: in that case multiple keys are in the same bucket, so we have to search among multiple keys. Worst case all the keys are in the same bucket, and thus we fallback on linear search.

    Hash collisions (with a large amount of keys) are however very rare. Usually it is safe to assume that - regardless of the size of the dictionary - the number of keys in the same bucket will be fixed.

    The fact that it is O(n) had some interesting consequences on security. Say you have a server that stores and retrieves data in a dictionary. Then of course the response time will scale with such a lookup. Now a hacker can design input in such a way that all keys are placed in the same bucket(s). As a result lookup will slow down, and eventually the server will not respond in a reasonable time anymore. That's why Python has a flag -R for hash randomization. This will change the hash function for every run and therefore make it harder for a hacker to design such input.

    0 讨论(0)
提交回复
热议问题