All problems
Hash Map with Separate Chaining
Problem
Simulate a hash map that resolves collisions with separate chaining.
There are bucket_count buckets indexed by key % bucket_count, and each bucket is a chain of key-value pairs. operations contains "put", "get", or "remove"; keys and values are parallel lists (values is only used by put). put records None, get returns the stored value or -1, and remove returns the removed value or -1 when the key is absent.
Return the list of recorded results.
Hint
Search the chain for the key first; put updates in place, remove unlinks the pair.
Test cases
case 1
in: [5,["put","put","get","put","get","remove","get"],[1,6,6,1,1,6,6],[10,20,0,30,0,0,0]]
expected: [null,null,20,null,30,20,-1]
case 2
in: [3,["get","remove"],[4,4],[0,0]]
expected: [-1,-1]
case 3
in: [1,["put","put","get","remove","get"],[2,3,2,3,3],[5,7,0,0,0]]
expected: [null,null,5,7,-1]
case 4
in: [4,["put","get"],[-2,-2],[9,0]]
expected: [null,9]
Starter codePython
def chained_hash_map(bucket_count, operations, keys, values):
# Your code here
pass4 test cases · run your code against them in the editor