class HashTable { size: number; table: (number | null)[]; constructor(size: number) { this.size = size; this.table = Array(size).fill(null); } hash(key: number): number { return key % this.size; } insert(key: number): void { let idx = this.hash(key); while (this.table[idx] !== null) { idx = (idx + 1) % this.size; } this.table[idx] = key; } } const ht = new HashTable(7); [10, 22, 31, 4, 15].forEach(k => ht.insert(k)); console.log(ht.table);
Each key is inserted at its hash index (key % 7). If occupied, linear probing moves to the next slot.
10 % 7 = 3 → slot 3 empty → insert 10 at index 3.
22 % 7 = 1 → slot 1 empty → insert 22 at index 1.
31 % 7 = 3 → slot 3 occupied (10), move to 4 → empty → insert 31 at index 4.
4 % 7 = 4 → slot 4 occupied (31), move to 5 → empty → insert 4 at index 5.
15 % 7 = 1 → slot 1 occupied (22), move to 2 → empty → insert 15 at index 2.
Final table: [null, 22, 15, 10, 31, 4, null]
class HashTable { size: number; table: (number | null)[]; constructor(size: number) { this.size = size; this.table = Array(size).fill(null); } hash(key: number): number { return key % this.size; } search(key: number): number { let idx = this.hash(key); let start = idx; while (this.table[idx] !== null) { if (this.table[idx] === key) return idx; idx = (idx + 1) % this.size; if (idx === start) break; } return -1; } } const ht = new HashTable(5); ht.table = [null, 12, 22, 32, 42]; console.log(ht.search(42));
Hash of 42 is 42 % 5 = 2.
Check index 2: 22 (not 42), move to 3: 32 (not 42), move to 4: 42 (found).
Return index 4.
insert(key: number): void {
let idx = this.hash(key);
while (this.table[idx] !== null) {
idx = (idx + 1) % this.size;
}
this.table[idx] = key;
}
// Assume this.table is full and insert is called.If the table is full, the while loop cycles through all indices endlessly because no null slot is found.
This causes an infinite loop.
Linear probing resolves collisions by checking the next sequential slot until an empty one is found.
This simplicity is its main advantage.
class HashTable { size: number; table: (number | 'DELETED' | null)[]; constructor(size: number) { this.size = size; this.table = Array(size).fill(null); } hash(key: number): number { return key % this.size; } insert(key: number): void { let idx = this.hash(key); while (this.table[idx] !== null && this.table[idx] !== 'DELETED') { idx = (idx + 1) % this.size; } this.table[idx] = key; } delete(key: number): void { let idx = this.hash(key); let start = idx; while (this.table[idx] !== null) { if (this.table[idx] === key) { this.table[idx] = 'DELETED'; return; } idx = (idx + 1) % this.size; if (idx === start) break; } } } const ht = new HashTable(5); ht.insert(7); ht.insert(12); ht.insert(17); ht.delete(12); ht.insert(22); console.log(ht.table);
Insert 7: 7 % 5 = 2 → slot 2 empty → insert at 2.
Insert 12: 12 % 5 = 2 → slot 2 occupied, move to 3 → empty → insert at 3.
Insert 17: 17 % 5 = 2 → slots 2 and 3 occupied, move to 4 → empty → insert at 4.
Delete 12: search from 2 → 2(7 no), 3(12 yes) → mark 3 as 'DELETED'.
Insert 22: 22 % 5 = 2 → 2 occupied, 3 'DELETED' → reuse 3 for 22.
Final table: [null, null, 7, 22, 17]
