Arrays and hashing interview questions
Arrays and hash maps are the first pattern in most coding interviews because they teach the habit behind many others: spend memory to save time.
How do I solve arrays and hashing interview questions?
Array and hashing questions are solved by remembering what you have already seen. A hash map or set turns a repeated search into a constant-time lookup, so a quadratic scan becomes linear. Use a map for counts, grouping, complements and prefix sums, then state the extra space.
How to recognise a arrays and hashing problem
The brute force compares every pair of elements, or searches the array again for each element.
The wording asks whether something has appeared before, how many times, whether two things match, or to group items that are equivalent.
You need a pair that adds up to a target, a duplicate, an anagram group, the most frequent element or a subarray with a given sum.
How to build the solution
Decide what to store as the key and what as the value: the number and its index, a sorted word and its group, a prefix sum and how often it occurred.
Walk the array once. For each element, check the map for what you need (a complement, a previous occurrence), then add the element. The order matters: check first, then insert, or an element pairs with itself.
For subarray sums, keep a running prefix sum and a map of prefix sums seen. A subarray ending here sums to k when the prefix minus k has been seen before.
State the result: time linear in the input, extra space linear in the number of distinct keys.
Classic problems to practise
Two Sum, Contains Duplicate, Valid Anagram, Group Anagrams, Top K Frequent Elements, Product of Array Except Self, Longest Consecutive Sequence, Subarray Sum Equals K.
Mistakes interviewers see
Inserting the element before checking, so it matches itself in a pair problem, or counting a duplicate twice.
Using a mutable or unhashable thing as a key, for example a list in Python, instead of a tuple or a sorted string.
Forgetting that hash maps are average-case constant time. Say so, and mention the worst case if asked.
Missing the empty array, a single element, all duplicates or negative numbers, which break prefix-sum and counting solutions.
Practise it as an interview
Take a topic-based interview on arrays and hashing to practise the pattern the way it is asked: say the pattern before you code, write and run the solution in a live editor, and answer the cross-questions about edge cases and complexity. The scored report shows whether you lost points explaining, coding or defending. Your first 3 single-question interviews are free for life, every interview report is free, and a new account also gets 6 welcome credits.
Frequently asked questions
When should I sort instead of using a hash map?
Sort when you can afford n log n and want constant extra space, or when order is the point. Use a hash map when you need linear time and can spend memory. Say which trade you chose and why.
What is the time complexity of a hash map lookup?
Average constant time. In the worst case, with many collisions, it can degrade to linear, though interview solutions assume the average.
Keep reading
- Two pointers interview questions
- Sliding window interview questions
- Stack interview questions
- Binary search interview questions
- Linked list interview questions
- Trees interview questions
- Graphs interview questions
- Heap and priority queue interview questions
- Company interview guidesRounds and questions by company