coding interview Tips: Applications
Top 3 Hash
map use
cases in
coding
interviews
Keys Hash Function Values
Find more interview tips ->
[Link]
Pair Sum Problems
In these kind of problems, we can use a hashmap to check if the
difference of a target and a number exists in an input list in O(1)
time.
This is an improvement over using an array, which would take us
O(n) time.
In an interview:
Use the value as the key and map it to its index. Loop over the list
and check if our map contains the difference we are looking for.
Find more
interview tips ->
[Link]
Duplicate Detection
Hash maps/sets are useful for detecting duplicates because of their
fast O(1) lookup. Using an array, this could potentially be O(n) time.
This is particularly useful in graph problems when we want to
avoid revisiting vertices.
In an interview:
At each iteration, check if the current element is already in our hash
map/set. If not, add, and move to the next iteration.
Find more
interview tips ->
[Link]
Counting
We can use the element as the key and map it to its frequency.
This is useful in problems where we have to check if we have
“enough” of something or problems where occurrences play a role
like anagrams or counting wins and losses.
In an interview:
Map each element (key) to its frequency (value), where the
frequency is initially set to 0. If we encounter a key again, we retrieve
its value and increment it by 1.
Find more
interview tips ->
[Link]
What’s another way
you’ve used a
hashmap?
[Link]