Problem · Hash Table

Custom Hash Map Operations

Learn this problem
MediumZoom logoZoomFULLTIMEONSITE INTERVIEW

Problem statement

Implement a custom integer hash map without using a built-in hash-map or dictionary type.

You are given parallel arrays operations and arguments. Process them from left to right on one initially empty map:

  • put has arguments [key, value]. Insert the pair, or replace the existing value for key.
  • get has arguments [key]. Append the stored value to the answer, or append -1 when the key is absent.
  • remove has arguments [key]. Delete the key when present; otherwise do nothing.

Return the values produced by the get operations, in operation order. Your implementation must resolve hash collisions by comparing keys, not by assuming each key has a unique bucket.

Function

runHashMap(operations: String[], arguments: int[][]) → int[]

Examples

Example 1

operations = ["put","put","get","put","get","remove","get"]arguments = [[1,10],[2,20],[1],[1,15],[1],[2],[2]]return = [10,15,-1]

The first get(1) returns 10. Updating key 1 changes its value to 15. After key 2 is removed, get(2) returns -1.

Example 2

operations = ["put","get","remove","remove","get"]arguments = [[0,0],[0],[0],[0],[0]]return = [0,-1]

A stored value of 0 is distinct from a missing key. Removing an already absent key has no effect.

Constraints

  • 1 <= operations.length <= 100000
  • operations.length == arguments.length
  • Each operation is exactly put, get, or remove.
  • put rows contain exactly two integers; other rows contain exactly one.
  • 0 <= key <= 10^9
  • 0 <= value <= 10^9

More Zoom problems

drafts saved locally
public int[] runHashMap(String[] operations, int[][] arguments) {
    // write your code here
}
operations["put","put","get","put","get","remove","get"]
arguments[[1,10],[2,20],[1],[1,15],[1],[2],[2]]
expected[10,15,-1]
checking account