Bird
Raised Fist0
DSA Javascriptprogramming~5 mins

Collision Handling Using Chaining in DSA Javascript - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Collision Handling Using Chaining
O(n/k)
Understanding Time Complexity

When we use chaining to handle collisions in a hash table, we want to know how fast operations like search, insert, and delete run.

We ask: How does the time to do these operations grow as the number of items increases?

Scenario Under Consideration

Analyze the time complexity of inserting and searching in a hash table using chaining.


class HashTable {
  constructor(size) {
    this.buckets = Array(size).fill(null).map(() => []);
  }

  hash(key) {
    return key.length % this.buckets.length;
  }

  insert(key, value) {
    const index = this.hash(key);
    this.buckets[index].push({ key, value });
  }

  search(key) {
    const index = this.hash(key);
    for (const pair of this.buckets[index]) {
      if (pair.key === key) return pair.value;
    }
    return null;
  }
}
    

This code stores key-value pairs in buckets. Each bucket is a list that holds all items with the same hash index.

Identify Repeating Operations

Look at what repeats when we insert or search:

  • Primary operation: Looping through the list (chain) at a bucket index.
  • How many times: Depends on how many items share the same bucket (chain length).
How Execution Grows With Input

As we add more items, chains get longer if the number of buckets stays the same.

Input Size (n)Approx. Operations (chain length)
10About 1-2 items per chain
100About 10 items per chain
1000About 100 items per chain

Pattern observation: If buckets stay fixed, chain length grows roughly proportional to n, making operations slower.

Final Time Complexity

Time Complexity: O(n/k) where k is number of buckets

This means the time depends on how many items share the same bucket. More buckets mean shorter chains and faster operations.

Common Mistake

[X] Wrong: "Hash table operations are always O(1) no matter what."

[OK] Correct: If many items collide into the same bucket, the chain grows and operations slow down, making it closer to O(n).

Interview Connect

Understanding how chaining affects time helps you explain hash table performance clearly, a key skill in coding interviews.

Self-Check

"What if we increase the number of buckets as we add items? How would that change the time complexity?"

Practice

(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Resize the hash table immediately after every insertion
B. Replace the existing element with the new one when a collision happens
C. Store multiple elements in a linked list at the same index when collisions occur
D. Use a secondary hash function to find a new index

Solution

  1. Step 1: Understand collision in hash tables

    When two keys hash to the same index, a collision occurs.
  2. Step 2: How chaining handles collisions

    Chaining stores all collided elements in a linked list at that index.
  3. Final Answer:

    Store multiple elements in a linked list at the same index when collisions occur -> Option C
  4. Quick Check:

    Chaining = linked list at index [OK]
Hint: Chaining means linked list at one index for collisions [OK]
Common Mistakes:
  • Thinking chaining replaces old values
  • Confusing chaining with open addressing
  • Assuming resizing happens on every collision
2. Which of the following is the correct way to initialize a hash table with chaining in JavaScript?
easy
A. const table = new Array(10).fill([]);
B. const table = Array.from({ length: 10 }, () => []);
C. const table = new Array(10);
D. const table = Array(10).fill(null);

Solution

  1. Step 1: Understand array initialization with unique lists

    Using fill([]) fills all indexes with the same array reference.
  2. Step 2: Use Array.from with a function to create unique arrays

    Array.from({ length: 10 }, () => []) creates a new empty array at each index.
  3. Final Answer:

    const table = Array.from({ length: 10 }, () => []); -> Option B
  4. Quick Check:

    Unique arrays per index = const table = Array.from({ length: 10 }, () => []); [OK]
Hint: Use Array.from with function for unique arrays [OK]
Common Mistakes:
  • Using fill([]) causes all indexes to share one array
  • new Array(10) leaves slots undefined
  • Filling with null does not create lists for chaining
3. Consider the following JavaScript code for inserting keys into a hash table with chaining:
const table = Array.from({ length: 5 }, () => []);
function hash(key) { return key % 5; }

// Insert keys
[7, 12, 17].forEach(key => {
  const index = hash(key);
  table[index].push(key);
});

console.log(table);

What is the output printed?
medium
A. [[7, 12, 17], [], [], [], []]
B. [[], [], [], [7, 12, 17], []]
C. [[], [7], [12], [17], []]
D. [[], [], [7, 12, 17], [], []]

Solution

  1. Step 1: Calculate hash index for each key

    7 % 5 = 2, 12 % 5 = 2, 17 % 5 = 2.
  2. Step 2: Insert keys at index 2

    All keys go into table[2], so table[2] = [7, 12, 17].
  3. Final Answer:

    [[], [], [7, 12, 17], [], []] -> Option D
  4. Quick Check:

    All keys at index 2 = [[], [], [7, 12, 17], [], []] [OK]
Hint: Calculate key % size to find index [OK]
Common Mistakes:
  • Miscomputing modulo results
  • Inserting keys at wrong indexes
  • Confusing array positions in output
4. The following code attempts to insert keys into a hash table with chaining but has a bug:
const table = new Array(3).fill([]);
function hash(key) { return key % 3; }

function insert(key) {
  const index = hash(key);
  table[index].push(key);
}

insert(1);
insert(4);
insert(7);
console.log(table);

What is the bug and how to fix it?
medium
A. Bug: All indexes share the same array; Fix: Use Array.from with () => []
B. Bug: hash function is incorrect; Fix: Use key % 10 instead
C. Bug: push method is wrong; Fix: Use unshift instead
D. Bug: table size is too small; Fix: Increase size to 10

Solution

  1. Step 1: Identify the bug with fill([])

    Using fill([]) assigns the same array reference to all indexes.
  2. Step 2: Fix by creating unique arrays per index

    Use Array.from({ length: 3 }, () => []) to create separate arrays.
  3. Final Answer:

    Bug: All indexes share the same array; Fix: Use Array.from with () => [] -> Option A
  4. Quick Check:

    fill([]) shares arrays, Array.from creates unique ones [OK]
Hint: Avoid fill([]); use Array.from with function [OK]
Common Mistakes:
  • Changing hash function unnecessarily
  • Using wrong array methods
  • Ignoring shared reference problem
5. You have a hash table of size 4 using chaining. You insert keys [3, 7, 11, 15, 19]. The hash function is key % 4. After all insertions, which index has the longest chain and what is its content?
medium
A. Index 3: [3, 7, 11, 15, 19]
B. Index 1: [3, 7, 11, 15, 19]
C. Index 3: [3, 7, 11, 15]
D. Index 3: [3, 7, 11]

Solution

  1. Step 1: Compute hash index for each key

    3 % 4 = 3, 7 % 4 = 3, 11 % 4 = 3, 15 % 4 = 3, 19 % 4 = 3.
  2. Step 2: Insert all keys at index 3

    All keys go into table[3], so chain is [3, 7, 11, 15, 19].
  3. Final Answer:

    Index 3: [3, 7, 11, 15, 19] -> Option A
  4. Quick Check:

    All keys mod 4 = 3, chain length 5 [OK]
Hint: All keys mod 4 equal 3, so chain at index 3 [OK]
Common Mistakes:
  • Miscomputing modulo for some keys
  • Assuming keys spread across indexes
  • Forgetting all keys collide at same index
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard