THINK FIRST·CODE LATER

← All labs

Linear Probing Table

Problem

Read the table size m, the number of keys n, and then n integer keys. Insert the keys in order into an empty hash table of size m using h(k) = Math.floorMod(k, m) and linear probing (h(k), h(k)+1, … wrapping round to 0).

  • If a key is already in the table, print Duplicate: K and skip it.
  • If all m slots are examined without finding a free one, print Table full: K not inserted.

Then print every slot as [i] key, or [i] - for an empty slot, followed by the total number of collisions (occupied slots skipped while inserting the keys that were stored) and the load as size/m.

Input:

11 6 22 35 13 46 57 24

Output:

[0] 22
[1] -
[2] 35
[3] 13
[4] 46
[5] 57
[6] 24
[7] -
[8] -
[9] -
[10] -
Collisions: 10
Load: 6/11

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.