Google Interview experience
1 round over 2 weeks. No offer, good experience.
I was asked a DSA question that involved implementing a solution to efficiently insert, search, and remove house addresses. The last 10-15 minutes were for me to ask any questions about the role and overall culture.
Question:
Implement a data structure to efficiently store and manage house addresses. Each house address consists of the following fields: House Number, Street Name, City , ZIP/Postal Code
Need to support the following operations:
Add address
Remove address
Search by house number
Search by street name (or prefix) (retrieves all houses for the street)
Search by zip code (or prefix) (retrieves all addresses located within a given zip/postal code)
Update address (house number, street name, or ZIP code of an existing address)
Solution:
To search for full addresses by house number, we can use a hashmap where the key is the house number and the value is the full address
address_by_house_number = {
1234: {"house_number": 1234, "street_name": "Elm Street", "zip_code": "12345"},
}We can make use of a trie for efficiently performing prefix-based searches on street names or ZIP codes. Each node in the Trie represents a character or digit, and the leaves (end nodes) contain references (house numbers) to addresses that match the prefix. At each node where the prefix search completes, we can gather all house numbers that are stored in that node and its descendants. The Trie’s search method will return a set of house numbers.
Once we have all relevant house numbers from the Trie, we can use the hashmap (address_by_house_number) to look up the full addresses for each of the house numbers.
Know someone interviewing at Google? Pass it on.
Same company, different candidates.
Share yours while it’s fresh. It takes five minutes, it’s anonymous, and it will help future candidates.