Software Engineering Intern Interview Question

How does a hash map work, and why are lookups usually so fast?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

A hash map stores key and value pairs in an array of buckets. A hash function turns each key into a number that picks a bucket, so finding a key means hashing it and looking in one place rather than searching everything, which is constant time on average. Collisions, where two keys land in the same bucket, are handled by chaining or probing, and the map resizes as it fills to keep buckets short.

Why interviewers ask this

Hash maps are the most useful data structure in coding interviews, so the interviewer wants to know you understand why they are fast and when they are not. They listen for hashing to a bucket, collision handling, resizing, and the difference between average and worst case. Knowing why keys should not change after insertion, or why a good hash function matters, shows you understand the mechanism rather than just the interface.

How to structure your answer

  • Explain buckets and the role of the hash function.
  • Describe what happens on a collision.
  • Explain resizing and the load factor.
  • Cover average versus worst case and what makes a good key.

Example answer

Spoken example, first person

Underneath, a hash map is an array of buckets. When I insert a key, a hash function turns it into a number, and that number, usually taken modulo the array size, picks a bucket. To look the key up later, I hash it again and go straight to that bucket, so instead of searching every element I check one spot. That is why lookups are constant time on average. Two different keys can land in the same bucket, which is a collision. One common approach is chaining, where each bucket holds a small list and you compare keys within it. Another is open addressing, where you probe for the next free slot. As more items go in, buckets get crowded, so when the ratio of items to buckets passes a threshold, the map allocates a bigger array and rehashes everything. That resize is expensive but rare enough that inserts stay constant time on average. The worst case is linear, if lots of keys collide. It is also why keys should not change after insertion: if a key's hash changes, the map looks in the wrong bucket and cannot find it.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • What makes a good hash function?
  • What happens if you use a mutable object as a key?
  • When would you use a tree based map instead?

Related software engineering intern questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot