I just read an interview question that asked for the fetching complexity of a hash key vs a hash value. I always figured both to be the same, at O(1 + n/k) (where k is number of buckets). What am I missing?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Fetching a hash key is O(lk) in the length of the key, because you have to hash it, but
n/kis supposed to be constant for any given hash table. This is usually referred to as O(1) as it does not depend onn, but it’s not strictly O(1) unless the key size is fixed.But fetching a hash value would require iterating through the whole table looking for it, assuming you didn’t pre-order it (you can design hash tables which can support binary lookups too for O(log(n)) but this is uncommon).