Skip to content
Sharp

Collections · interview question

How would you explain the time complexity of checking if an element exists in a HashSet, assuming a good hash function in an interview?

Multiple choiceMediumchapter-4-curatedpremiumcollectionsset-interface-and-hashset
Choose one answer
Answer and explanation

Correct answer

A. O(1)

Why

With a good hash function, checking if an element exists in a HashSet is O(1), as it directly accesses the bucket where the element would be placed.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →