AlgoFrames

Fill a table with linear probing

Standard · Write code · about 10 min

A table has size slots and starts empty. Insert the distinct keys in keys in order with linear probing: a key starts at slot key mod size, and while that slot is taken it moves to the next slot, wrapping from the last slot to slot 0.

Return the table as an array of length size, with −1 for every empty slot.

Example 1

Input:

[[3,11,5,19],8]

Output:

[-1,-1,-1,3,11,5,19,-1]

19 starts at slot 3 and probes 4 and 5 before it finds slot 6 free.

Example 2

Input:

[[6,14,22],8]

Output:

[22,-1,-1,-1,-1,-1,6,14]

22 wraps around from slot 7 to slot 0.

Constraints

  • 0 ≤ keys.length ≤ size ≤ 10,000
  • keys are distinct, 0 ≤ key ≤ 1,000,000

Learn the idea first: Open addressing: linear, quadratic probing, double hashing, a visual lesson.

More Open addressing: linear, quadratic probing, double hashing problems