AlgoFrames

Implement remove and iteration

Challenge · Write code · about 20 min

Extend the chained hash table with deletion and iteration. Use 8 buckets of [key, value] pairs and hash with key mod 8. New keys go to the end of their chain. Do not use a built-in map.

Commands: put K V stores or replaces a value and prints nothing. get K prints the value, or "-1" if missing. remove K unlinks K and prints "true", or "false" if missing. keys prints every key separated by single spaces, in bucket order 0 to 7 and in chain order inside each bucket (empty text when the table is empty).

Return the printed lines in order.

Example 1

Input:

[["put 1 10","put 9 90","put 2 20","keys","remove 1","keys","remove 5"]]

Output:

["1 9 2","true","9 2","false"]

Bucket 1 holds 1 then 9, bucket 2 holds 2.

Example 2

Input:

[["put 4 1","get 4","remove 4","get 4"]]

Output:

["1","true","-1"]

Constraints

  • 0 ≤ commands.length ≤ 10,000
  • 0 ≤ K ≤ 1,000,000 and V is a whole number written as text
  • Replacing a value keeps the key at its place in the chain

Learn the idea first: Hash tables with separate chaining, a visual lesson.

More Hash tables with separate chaining problems