Bird
0
0
DSA Cprogramming~10 mins

Two Sum Problem Classic Hash Solution 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 hash map for storing numbers and their indices.

DSA C
int hashMap[[1]];
Drag options to blanks, or click blank then click option'
A100
B1000
C10
D100000
Attempts:
3 left
💡 Hint
Common Mistakes
Using too small a size causing collisions or out-of-bound errors.
Using an uninitialized or zero-sized array.
2fill in blank
medium

Complete the code to initialize the hash map values to -1 indicating empty slots.

DSA C
for (int i = 0; i < [1]; i++) {
    hashMap[i] = -1;
}
Drag options to blanks, or click blank then click option'
A100000
B1000
C100
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing fewer elements than the hash map size.
Not initializing the hash map at all.
3fill in blank
hard

Fix the error in the condition to check if the complement exists in the hash map.

DSA C
if (hashMap[[1]] != -1) {
    return (int[]){hashMap[complement], i};
}
Drag options to blanks, or click blank then click option'
Acomplement
Bnums[i]
Ci
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Checking hashMap[nums[i]] instead of hashMap[complement].
Using the wrong variable in the condition.
4fill in blank
hard

Fill both blanks to store the current index in the hash map and calculate the complement.

DSA C
int complement = [1] - nums[i];
hashMap[[2]] = i;
Drag options to blanks, or click blank then click option'
Atarget
Bnums[i]
Ci
Dcomplement
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping complement and nums[i] in assignments.
Storing complement instead of nums[i] in hash map.
5fill in blank
hard

Fill all three blanks to complete the Two Sum function returning indices of the two numbers.

DSA C
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    static int hashMap[100000];
    for (int i = 0; i < [1]; i++) {
        hashMap[i] = -1;
    }
    for (int i = 0; i < numsSize; i++) {
        int complement = [2] - nums[i];
        if (hashMap[complement] != -1) {
            int* result = malloc(2 * sizeof(int));
            result[0] = hashMap[complement];
            result[1] = i;
            *returnSize = 2;
            return result;
        }
        hashMap[[3]] = i;
    }
    *returnSize = 0;
    return NULL;
}
Drag options to blanks, or click blank then click option'
A100000
Btarget
Cnums[i]
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop size for initialization.
Mixing up complement and nums[i] in hash map access.
Not allocating memory for the result array.