Linear Probing: Exercise I

  • Describe "Open Addressing with Linear Probing" as a collision resolution.

Suppose we have a hash table with capacity M=7M=7, and we aim to insert integer keys. Further, assume the hashCode() is defined as the sum of the digits of key.

Exercise Complete the table after each of the following operations, assuming collision resolution using linear probing (with step size of 11). For the find() operation, output the indices of the positions visited during the search to find the element.

[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ]Output
insert(1111)
insert(5005)
insert(86)
find(5557)
insert(2332)
insert(8333)
find(2332)
insert(700)
Solution
[ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ]Output
insert(1111)1111
insert(5005)50051111
insert(86)8650051111
find(5557)86500511111: NOT_FOUND
insert(2332)86500511112332
insert(8333)865005111123328333
find(2332)8650051111233283333,4,5: FOUND
insert(700)867005005111123328333

(1+1+1+1)mod7=4 (1 + 1 + 1 + 1) \bmod 7 = 4

Therefore, insert 11111111 at index 44.

(5+0+0+5)mod7=3 (5 + 0 + 0 + 5) \bmod 7 = 3

Therefore, insert 50055005 at index 33.

(8+6)mod7=0 (8 + 6) \bmod 7 = 0

Therefore, insert 8686 at index 00.

(5+5+5+7)mod7=1 (5 + 5 + 5 + 7) \bmod 7 = 1

Lookup for 55575557 at index 11. There is no element there! Therefore, return NOT_FOUND.

(2+3+3+2)mod7=3 (2 + 3 + 3 + 2) \bmod 7 = 3

Try to insert 23322332 at index 33. The position is occupied. Linear probing will take us to index 44 and then index 55 where the element can be inserted. Therefore, insert 23322332 at index 55.

(8+3+3+3)mod7=3 (8 + 3 + 3 + 3) \bmod 7 = 3

Try to insert 83338333 at index 33. The position is occupied, but the occupant is not the target! Linear probing will take us to index 44 and then index 55 and finally index 66 where the element can be inserted. Therefore, insert 83338333 at index 66.

(2+3+3+2)mod7=3 (2 + 3 + 3 + 2) \bmod 7 = 3

Lookup 23322332 at index 33. The position is occupied, but the occupant is not the target! Linear probing will take us to index 44 and then index 55 where the element can be found.

(7+0+0)mod7=0 (7 + 0 + 0) \bmod 7 = 0

Try to insert 700700 at index 00. The position is occupied. Linear probing will take us to index 11 where the element can be inserted. Therefore, insert 700700 at index 11.