When would you reach for a hash set or hash map to solve an array problem, and what does it buy you over scanning?

technical-conceptual · Junior level · software-engineering

What the interviewer is really asking

Assesses whether the candidate recognizes hashing as the trade of space for time, and can apply it to membership, dedup, and complement-lookup patterns.

What to say

What to avoid

Example answers

Strong: Hashing trades memory for speed: an O(1) average lookup lets me replace a repeated inner scan. For two-sum I make one pass, and for each number I check whether `target - num` is already in the map before inserting `num` — that's O(n) time and O(n) space instead of the O(n^2) brute force of checking every pair. The map also hands me the index for free so I can return positions.

Weak: I'd use a hash map because they're fast — pretty much always the right tool for array problems.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.