Bird
0
0
DSA Cprogramming~10 mins

HashMap Implementation from Scratch in DSA C - Interactive Practice

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

Complete the code to declare the size of the hash map array.

DSA C
const int TABLE_SIZE = [1];
Drag options to blanks, or click blank then click option'
A10
B1
C100
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or one as table size causes no space or too small space.
2fill in blank
medium

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

DSA C
unsigned int hash(char *key) {
    unsigned int hash = 0;
    for (int i = 0; key[i] != '\0'; i++) {
        hash += key[i];
    }
    return hash [1] TABLE_SIZE;
}
Drag options to blanks, or click blank then click option'
A*
B/
C%
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) can produce indices larger than TABLE_SIZE, causing out-of-bounds access.
3fill in blank
hard

Fix the error in the insert function to correctly add a new key-value pair.

DSA C
void insert(HashMap *map, char *key, int value) {
    unsigned int index = hash(key);
    Entry *new_entry = malloc(sizeof(Entry));
    new_entry->key = key;
    new_entry->value = value;
    new_entry->next = map->table[[1]];
    map->table[index] = new_entry;
}
Drag options to blanks, or click blank then click option'
Amap
Bvalue
Ckey
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' or 'key' instead of the index causes wrong memory access.
4fill in blank
hard

Fill both blanks to complete the search function that finds a value by key.

DSA C
int search(HashMap *map, char *key) {
    unsigned int index = hash(key);
    Entry *current = map->table[[1]];
    while (current != NULL) {
        if (strcmp(current->key, [2]) == 0) {
            return current->value;
        }
        current = current->next;
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
Aindex
Bkey
Cmap
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for bucket or key comparison.
5fill in blank
hard

Fill all three blanks to complete the delete function that removes a key from the map.

DSA C
void delete(HashMap *map, char *key) {
    unsigned int index = hash(key);
    Entry *current = map->table[index];
    Entry *prev = NULL;
    while (current != NULL) {
        if (strcmp(current->key, [1]) == 0) {
            if ([2] == NULL) {
                map->table[index] = current->next;
            } else {
                [3]->next = current->next;
            }
            free(current);
            return;
        }
        prev = current;
        current = current->next;
    }
}
Drag options to blanks, or click blank then click option'
Akey
Bprev
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for key comparison or pointer updates.