Technically speaking, based on posts I\'ve read here, Hash table is indeed O(n) time lookup in the worst case. But I don\'t get how the internal mechanics guarantee it to be
Great question!
Assume
strings to valueshashFunction(string) => hashedIndex : int in O(1) valueArray : [any] stores valuesvalueIndex : int is the first empty index in valueArraylookupArray : [int] stores each valueIndex at hashedIndex// Setting a value
valueArray[valueIndex] = value
hashedIndex = hashFunction(string)
lookupArray[hashedIndex] = valueIndex
// Looking up a value
hashedIndex = hashFunction(string) // O(1)
valueIndex = lookupArray[hashedIndex]; // O(1) array lookup
value = valueArray[valueIndex]; // O(1) array lookup
Lots of details omitted to answer your question clearly.
Hope that helps!