Skip to content

QuizNot attempted

Hash map costs

What is the average and worst-case lookup time of a hash map?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • O(1) average, O(n) worst case (everything collides)
  • A good hash spreads keys across buckets, so lookup is O(1) on average
  • if every key collides into one bucket it degrades to scanning that bucket, O(n).
  • Not O(log n) always: that is a balanced tree, not a hash table.
  • Not O(1) always: collisions make the worst case linear (which is why some runtimes randomise hash seeds).
  • Not O(n) average: only the pathological case is linear.
  • JS Map/Set also preserve insertion order.