184k views
1 vote
What is the worst case run time of:

a. ArrayList.get()
b. HashSet.contains()
c. LinkedList.indexOf()

1 Answer

1 vote

Final answer:

The worst case runtime of ArrayList.get() is O(1), HashSet.contains() is O(1) on average but can be O(n) in the worst case scenario, and LinkedList.indexOf() is O(n).

Step-by-step explanation:

The worst case runtime of ArrayList.get() is O(1), which means it has a constant runtime regardless of the size of the list. This is because the ArrayList internally uses an array to store elements, and getting an element at a specific index can be done in constant time by accessing the corresponding array index.

The worst case runtime of HashSet.contains() is O(1) on average, but it can be O(n) in the worst case scenario. This is because in a HashSet, elements are stored in a hash table and the contains() operation involves calculating the hash value of the element and searching for it in the hash table. In the worst case, all elements might hash to the same index, resulting in a linear search.

The worst case runtime of LinkedList.indexOf() is O(n), where n is the number of elements in the linked list. This is because a linked list does not provide direct access to elements by index, so finding the index of an element requires traversing the list from the beginning until the element is found.

User Leroy Kegan
by
8.6k points