Problem · Hash Table

Design Hash Map

Learn this problem
EasyApple logoAppleFULLTIMEPHONE SCREEN

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.

Process the arrays from left to right. At index i:

  • "put" stores values[i] under keys[i]. If the key already exists, replace its value.
  • "get" appends the value stored under keys[i] to the result, or appends -1 when the key is absent.
  • "remove" deletes keys[i] when present. Removing a missing key has no effect.

Return an integer array containing the results of all get operations in encounter order.

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 queries return 10 and -1. Updating key 2 changes its value to 25, and removing it makes the final query 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 missing key 0 has no effect. The first insertion stores 0, the second insertion replaces it with 7, and the last query occurs after removal.

Constraints

  • 1 <= operations.length <= 100000.
  • operations.length == keys.length == values.length.
  • Every operation is "put", "get", or "remove".
  • 0 <= keys[i] <= 1000000.
  • For every put operation, 0 <= values[i] <= 1000000.

More Apple problems

drafts saved locally
public int[] runHashMap(String[] operations, int[] keys, int[] values) {
  // write your code here
}
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]
expected[10,-1,25,-1]
checking account