Suppose you insert() an item into a hashtable and then immediately do a lookup()
ID: 3704243 • Letter: S
Question
Suppose you insert() an item into a hashtable and then immediately do a
lookup() on that item as the methods shown below. What is the worst-case complexity of the program in this
situation? Briefly explain the answer please. Thanks!
Explanation / Answer
For any search, the worst-case running time occurs when the desired item is not in the array.
The worst-case lookup time in hash table schemes is O(n)
Explaination:
When you have inserted the element, a hash index is assigned to it and lets say it lies at position n in the list.
Now, in the lookup function, we are accessing each element and check if it is equal to the item we are looking for. Hence, nth item will be found after checking n values. Hence it will be n.
Even if the element is not present in the array then, n is total number of elements in the list, as we iterate over full list to find the element.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.