Bird
Raised Fist0
DSA C++programming~20 mins

Collision Handling Using Chaining in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Chaining Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of inserting keys in a chained hash table
What is the printed state of the hash table after inserting keys 10, 22, 31, 4, 15, 28, 17, 88, 59 using chaining with hash function h(key) = key % 10 and table size 10?
DSA C++
class HashTable {
public:
    static const int TABLE_SIZE = 10;
    std::list<int> table[TABLE_SIZE];

    int hashFunction(int key) {
        return key % TABLE_SIZE;
    }

    void insert(int key) {
        int index = hashFunction(key);
        table[index].push_back(key);
    }

    void printTable() {
        for (int i = 0; i < TABLE_SIZE; i++) {
            std::cout << i << " -> ";
            for (int key : table[i]) {
                std::cout << key << " -> ";
            }
            std::cout << "null" << std::endl;
        }
    }
};

int main() {
    HashTable ht;
    int keys[] = {10, 22, 31, 4, 15, 28, 17, 88, 59};
    for (int key : keys) {
        ht.insert(key);
    }
    ht.printTable();
    return 0;
}
A{"0": "10 -> null", "1": "31 -> null", "2": "22 -> null", "3": "null", "4": "4 -> null", "5": "15 -> null", "6": "null", "7": "17 -> null", "8": "28 -> 88 -> null", "9": "59 -> null"}
B}"llun >- 95" :"9" ,"llun >- 88 >- 82" :"8" ,"llun >- 71" :"7" ,"llun" :"6" ,"llun >- 51" :"5" ,"llun >- 4" :"4" ,"llun" :"3" ,"llun >- 22" :"2" ,"llun >- 13" :"1" ,"llun >- 01" :"0"{
C{"0": "10 -> null", "1": "31 -> null", "2": "22 -> null", "3": "null", "4": "4 -> null", "5": "15 -> null", "6": "null", "7": "17 -> null", "8": "28 -> null", "9": "59 -> 88 -> null"}
D{"0": "10 -> null", "1": "31 -> 41 -> null", "2": "22 -> null", "3": "null", "4": "4 -> 14 -> null", "5": "15 -> null", "6": "null", "7": "17 -> null", "8": "28 -> 88 -> null", "9": "59 -> null"}
Attempts:
2 left
💡 Hint
Calculate key % 10 for each key and add it to the linked list at that index.
🧠 Conceptual
intermediate
1:30remaining
Understanding collision handling with chaining
Why is chaining a good method to handle collisions in a hash table?
ABecause it sorts all keys in the hash table to avoid collisions.
BBecause it uses open addressing to find the next free slot for collided keys.
CBecause it increases the size of the hash table dynamically to prevent collisions.
DBecause it stores all collided keys in a linked list at the same index, allowing multiple keys to share one slot.
Attempts:
2 left
💡 Hint
Think about how multiple keys can be stored at the same index.
🔧 Debug
advanced
1:30remaining
Identify the error in chaining insertion code
What error will occur when running this code snippet for inserting a key into a chained hash table? std::list table[10]; int hashFunction(int key) { return key % 10; } void insert(int key) { int index = hashFunction(key); table[index].push_back(key); } Assuming this is part of a class and called correctly.
ACompilation error: 'table' is not declared in this scope.
BRuntime error: segmentation fault due to uninitialized list.
CNo error; code runs correctly and inserts key.
DLogic error: keys are inserted at wrong index.
Attempts:
2 left
💡 Hint
Check if the list array is properly declared and used.
❓ Predict Output
advanced
2:00remaining
Output after deleting a key from chained hash table
Given a chained hash table with keys 10, 20, 30 inserted using h(key) = key % 10, what is the printed table after deleting key 20?
DSA C++
class HashTable {
public:
    static const int TABLE_SIZE = 10;
    std::list<int> table[TABLE_SIZE];

    int hashFunction(int key) {
        return key % TABLE_SIZE;
    }

    void insert(int key) {
        int index = hashFunction(key);
        table[index].push_back(key);
    }

    void remove(int key) {
        int index = hashFunction(key);
        table[index].remove(key);
    }

    void printTable() {
        for (int i = 0; i < TABLE_SIZE; i++) {
            std::cout << i << " -> ";
            for (int key : table[i]) {
                std::cout << key << " -> ";
            }
            std::cout << "null" << std::endl;
        }
    }
};

int main() {
    HashTable ht;
    ht.insert(10);
    ht.insert(20);
    ht.insert(30);
    ht.remove(20);
    ht.printTable();
    return 0;
}
A{"0": "10 -> null", "1": "null", "2": "null", "3": "null", "4": "null", "5": "null", "6": "null", "7": "null", "8": "null", "9": "30 -> null"}
B{"0": "10 -> 30 -> null", "1": "null", "2": "null", "3": "null", "4": "null", "5": "null", "6": "null", "7": "null", "8": "null", "9": "null"}
C{"0": "10 -> null", "1": "null", "2": "null", "3": "null", "4": "null", "5": "null", "6": "null", "7": "null", "8": "null", "9": "null"}
D{"0": "10 -> 20 -> 30 -> null", "1": "null", "2": "null", "3": "null", "4": "null", "5": "null", "6": "null", "7": "null", "8": "null", "9": "null"}
Attempts:
2 left
💡 Hint
20 % 10 = 0, so 20 is in the list at index 0 and should be removed.
🚀 Application
expert
1:30remaining
Analyzing average search time in chaining hash table
If a hash table uses chaining and has 1000 keys stored in 100 buckets, what is the average number of keys checked during a successful search assuming uniform distribution?
A10
B100
C1
D0
Attempts:
2 left
💡 Hint
Average chain length = total keys / number of buckets.

Practice

(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Store all elements that hash to the same index in a linked list at that index
B. Resize the hash table when a collision occurs
C. Use a second hash function to find a new index
D. Ignore collisions and overwrite existing data

Solution

  1. Step 1: Understand collision in hash tables

    When two keys hash to the same index, a collision occurs.
  2. Step 2: Explain chaining method

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

    Store all elements that hash to the same index in a linked list at that index -> Option A
  4. Quick Check:

    Chaining = linked list at index [OK]
Hint: Chaining means linked list at each hash index [OK]
Common Mistakes:
  • Thinking chaining resizes the table
  • Confusing chaining with open addressing
  • Ignoring collisions instead of handling them
2. Which of the following is the correct way to declare a hash table using chaining in C++?
easy
A. std::vector<int> hashTable(size);
B. std::list hashTable(size);
C. std::vector hashTable(size);
D. std::list<int> hashTable(size);

Solution

  1. Step 1: Understand chaining data structure

    Chaining uses a list at each index, so a vector of lists is needed.
  2. Step 2: Check syntax correctness

    std::vector creates a vector where each element is a list, perfect for chaining.
  3. Final Answer:

    std::vector hashTable(size); -> Option C
  4. Quick Check:

    Vector of lists = chaining hash table [OK]
Hint: Chaining = vector of lists in C++ [OK]
Common Mistakes:
  • Using list of vectors instead of vector of lists
  • Using vector of ints only (no chaining)
  • Incorrect container nesting order
3. Consider the following code snippet using chaining for collision handling:
std::vector hashTable(5);
int hashFunction(int key) { return key % 5; }

// Insert keys
hashTable[hashFunction(7)].push_back(7);
hashTable[hashFunction(12)].push_back(12);
hashTable[hashFunction(17)].push_back(17);

// Print contents at index 2
for (int val : hashTable[2]) {
    std::cout << val << " ";
}

What will be the output?
medium
A. 7 12 17
B. 12 17
C. 7
D. No output

Solution

  1. Step 1: Calculate hash indices for keys

    7 % 5 = 2, 12 % 5 = 2, 17 % 5 = 2. All keys go to index 2.
  2. Step 2: Check what is printed at index 2

    All three keys (7, 12, 17) are stored in the list at index 2 and printed in insertion order.
  3. Final Answer:

    7 12 17 -> Option A
  4. Quick Check:

    Keys mod 5 = 2, print all at index 2 [OK]
Hint: Calculate key % size to find index [OK]
Common Mistakes:
  • Forgetting all keys collide at index 2
  • Printing keys at wrong index
  • Assuming keys overwrite each other
4. The following code attempts to insert keys into a hash table using chaining but causes a runtime error:
std::vector hashTable;
int hashFunction(int key) { return key % 10; }

void insertKey(int key) {
    int index = hashFunction(key);
    hashTable[index].push_back(key);
}

int main() {
    insertKey(15);
    insertKey(25);
    return 0;
}

What is the error and how to fix it?
medium
A. hashFunction returns wrong index; change modulo to 5
B. hashTable is empty; initialize with size before insertions
C. push_back is wrong method; use insert instead
D. No error; code runs fine

Solution

  1. Step 1: Identify cause of runtime error

    hashTable is declared but not sized, so accessing hashTable[index] causes out-of-range error.
  2. Step 2: Fix by initializing hashTable size

    Initialize hashTable with size 10 (e.g., std::vector hashTable(10);) before insertions.
  3. Final Answer:

    hashTable is empty; initialize with size before insertions -> Option B
  4. Quick Check:

    Vector size must match hash range [OK]
Hint: Initialize vector size before accessing indices [OK]
Common Mistakes:
  • Ignoring vector size causes crash
  • Changing hash function unnecessarily
  • Using wrong list methods
5. You have a hash table of size 7 using chaining. You insert keys: 10, 21, 17, 14, 28, 35.
After all insertions, which index will have the longest chain and what will be its contents?
Use hash function: index = key % 7.
hard
A. Index 0: 10 -> 21 -> 35
B. Index 3: 10 -> 17
C. Index 4: 21 -> 14
D. Index 0: 21 -> 14 -> 28 -> 35

Solution

  1. Step 1: Calculate hash indices for each key

    10 % 7 = 3, 21 % 7 = 0, 17 % 7 = 3, 14 % 7 = 0, 28 % 7 = 0, 35 % 7 = 0.
  2. Step 2: Group keys by index

    Index 0: 21, 14, 28, 35 (4 keys); Index 3: 10, 17 (2 keys).
  3. Step 3: Identify longest chain and contents

    Index 0 has the longest chain with keys 21, 14, 28, 35 inserted in order.
  4. Final Answer:

    Index 0: 21 -> 14 -> 28 -> 35 -> Option D
  5. Quick Check:

    Keys mod 7 grouped; longest chain at index 0 [OK]
Hint: Calculate key % size, count keys per index [OK]
Common Mistakes:
  • Miscomputing modulo results
  • Mixing up indices and keys
  • Ignoring insertion order in chain
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