Design Hash Map
Updated
Problem statement
Implement a hash map that stores integer values under integer keys. Build the storage yourself without using a language's built-in hash table, map, or dictionary for the stored entries.
You get the arrays operations, keys, and values. Go through them from left to right. At index i, operations[i] says what to do:
"put"storesvalues[i]underkeys[i]. If the key already exists, replace its value."get"appends the value stored underkeys[i]to the result. If the key is absent, it appends-1instead. Call this the not-found value."remove"deleteskeys[i]when present. Removing a missing key has no effect.
Return an integer array with the results of all get operations, in the order they ran.
Function
runHashMap(operations: String[], keys: int[], values: int[]) → int[]Examples
Example 1
operations = ["put","put","get","get","put","get","remove","get"]keys = [1,2,1,3,2,2,2,2]values = [10,20,0,0,25,0,0,0]return = [10,-1,25,-1]The first two get operations return 10 and -1, since the second asks for a key that was never stored. The next put changes the value of key 2 to 25, and the next get returns that value. Removing the key then makes the last get return -1.
Example 2
operations = ["remove","put","get","put","get","remove","get"]keys = [0,0,0,0,0,0,0]values = [0,0,0,7,0,0,0]return = [0,7,-1]Removing the missing key 0 at the start has no effect. The first put stores the value 0, and the next get returns it. The second put replaces it with 7, which the next get returns. The key is then removed, so the last get returns the not-found value.
Constraints
1 <= operations.length <= 100000.operations.length == keys.length == values.length.- Every operation is
"put","get", or"remove". 0 <= keys[i] <= 1000000.- For every
putoperation,0 <= values[i] <= 1000000.
More Apple Phone Screen questions
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
- Maintain a Consistent-Hashing RingPHONE SCREEN · Seen Oct 2026
- Count Nodes in a ChainPHONE SCREEN · Seen Oct 2026
- Run-Length DecodingPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Config Store with Priority OverridesMicrosoft · PHONE SCREENMicrosoft interview questions
- ID Allocator with Assign and ReleaseSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Feature Flag EvaluationCVS Health · PHONE SCREEN
- Bounded Service Instance RegistryRevolut · PHONE SCREENRevolut interview questions
- In-Memory Key-Value Store OperationsOpenAI · PHONE SCREENOpenAI interview questions
- In-Memory Fixed-Window Rate LimiterRippling · PHONE SCREENRippling interview questions
- Design an Ordered StreamBloomberg LP · OA · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)