Build a Hash Table
Updated
Problem statement
Build a hash table that maps string keys to integer values. Write the storage yourself: do not use your language's built-in hash table, map, dictionary or set to hold the entries.
You get three arrays of the same length: operations, keys and values. Run the operations from left to right. At index i, operations[i] is one of these:
"put": storevalues[i]under the keykeys[i]. If the key is already in the table, replace its value."get": look upkeys[i]and add its value to the result, or add-1if the key is not in the table."remove": deletekeys[i]and its value from the table. If the key is not in the table, do nothing.
Return the results of all "get" operations, in the order they ran.
How you spread the keys over your storage, and what you do when two different keys land in the same place, is up to you. Either way, every key must keep its own value.
Function
runHashTable(operations: String[], keys: String[], values: int[]) → int[]Examples
Example 1
operations = ["put","put","get","put","get","get"]keys = ["apple","pear","apple","apple","apple","plum"]values = [3,7,0,5,0,0]return = [3,5,-1]The first "get" finds 3 under "apple". The next "put" replaces that value with 5, so the second "get" returns 5. The key "plum" was never stored, so the last "get" returns -1.
Example 2
operations = ["put","put","get","get","remove","get","get"]keys = ["ab","ba","ab","ba","ab","ab","ba"]values = [1,2,0,0,0,0,0]return = [1,2,-1,2]The keys "ab" and "ba" use the same letters, but they are different keys, so the first two lookups return 1 and 2. After "ab" is removed, looking it up returns -1, while "ba" still returns 2.
Example 3
operations = ["get","remove","put","get","remove","get"]keys = ["home","home","home","home","home","home"]values = [0,0,0,0,0,0]return = [-1,0,-1]The table starts empty, so the first "get" returns -1, and removing the missing key changes nothing. Then "home" is stored with the value 0, which is a real value, so the next lookup returns 0. After the key is removed, the last lookup returns -1.
Constraints
1 <= operations.length <= 10000keys.length == operations.lengthvalues.length == operations.length- Each
operations[i]is"put","get"or"remove". 1 <= keys[i].length <= 10keys[i]contains only lowercase English letters.0 <= values[i] <= 1000000000- When
operations[i]is"get"or"remove",values[i]is0and is not used.
More Zillow interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- First Unique Character in a StringOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- First N Fibonacci NumbersOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2021
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Blackjack Hand ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2016
- FactorialPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2014
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Cumulative Fibonacci SumOA · PHONE SCREEN · Seen Aug 2021
- Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Sep 2019
- Convert a String to a LongOA · Seen Jan 2018
Similar questions at other companies
- Highest Bid Campaign MatcherStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Design a Food Rating SystemApple · PHONE SCREENApple interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Hierarchical In-Memory File SystemClay · PHONE SCREEN
- Design File SystemGoogle · PHONE SCREENGoogle interview questions
- Numeric Poker OperationsRippling · PHONE SCREENRippling interview questions