Bird
0
0
DSA Cprogramming~10 mins

Why Hash Map Exists and What Problem It Solves in DSA C - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a hash map that stores integer keys and values.

DSA C
typedef struct {
    int key;
    int value;
} HashMapEntry;

HashMapEntry map[[1]];
Drag options to blanks, or click blank then click option'
Ahash
Bkey
C100
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names instead of a number for array size.
Confusing key or value with size.
2fill in blank
medium

Complete the code to compute the hash index for a given key.

DSA C
int hashFunction(int key) {
    return key [1] 100;
}
Drag options to blanks, or click blank then click option'
A%
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or addition instead of modulo.
Not limiting the index within array bounds.
3fill in blank
hard

Fix the error in the code that inserts a key-value pair into the hash map.

DSA C
void insert(HashMapEntry map[], int key, int value) {
    int index = hashFunction(key);
    map[index].key = key;
    map[index].[1] = value;
}
Drag options to blanks, or click blank then click option'
Ahash
Bkey
Cindex
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to the key field instead of value.
Using wrong field names.
4fill in blank
hard

Fill both blanks to complete the code that retrieves a value by key from the hash map.

DSA C
int getValue(HashMapEntry map[], int key) {
    int index = hashFunction(key);
    if (map[index].key == [1]) {
        return map[index].[2];
    }
    return -1; // not found
}
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Cindex
Dhash
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with wrong variable.
Returning the key instead of value.
5fill in blank
hard

Fill all three blanks to complete the code that handles collision by linear probing in the hash map insertion.

DSA C
void insert(HashMapEntry map[], int key, int value) {
    int index = hashFunction(key);
    while (map[index].key != -1 && map[index].key != [1]) {
        index = (index [2] 1) [3] 100;
    }
    map[index].key = key;
    map[index].value = value;
}
Drag options to blanks, or click blank then click option'
Akey
B+
C%
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators for index calculation.
Not handling wrap-around with modulo.