Bird
0
0
DSA Cprogramming~20 mins

Two Sum Problem Classic Hash Solution in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Two Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Two Sum Classic Hash Solution
What is the output of the following C code that solves the Two Sum problem using a hash map approach?
DSA C
#include <stdio.h>
#include <stdlib.h>

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int* hash = (int*)calloc(2048, sizeof(int));
    int* result = (int*)malloc(2 * sizeof(int));
    for (int i = 0; i < 2048; i++) hash[i] = -1;
    for (int i = 0; i < numsSize; i++) {
        int complement = target - nums[i];
        int hashIndex = complement & 2047;
        if (hash[hashIndex] != -1 && nums[hash[hashIndex]] == complement) {
            result[0] = hash[hashIndex];
            result[1] = i;
            *returnSize = 2;
            free(hash);
            return result;
        }
        hash[nums[i] & 2047] = i;
    }
    *returnSize = 0;
    free(hash);
    free(result);
    return NULL;
}

int main() {
    int nums[] = {2, 7, 11, 15};
    int target = 9;
    int returnSize = 0;
    int* indices = twoSum(nums, 4, target, &returnSize);
    if (indices != NULL) {
        printf("%d -> %d -> null\n", nums[indices[0]], nums[indices[1]]);
        free(indices);
    } else {
        printf("No solution found\n");
    }
    return 0;
}
A11 -> 15 -> null
B7 -> 2 -> null
C2 -> 7 -> null
DNo solution found
Attempts:
2 left
💡 Hint
Think about which two numbers add up to the target 9 in the array.
🧠 Conceptual
intermediate
1:30remaining
Understanding Hash Map Usage in Two Sum
In the classic hash solution for Two Sum, why do we use a hash map to store indices of numbers?
ATo sort the array before searching for pairs
BTo reverse the array for easier traversal
CTo store the sum of all pairs for later use
DTo quickly check if the complement of the current number exists in the array
Attempts:
2 left
💡 Hint
Think about how to find pairs that add up to the target efficiently.
🔧 Debug
advanced
1:30remaining
Identify the Error in Hash Index Calculation
What error will occur in this hash index calculation line in the Two Sum hash solution? int hashIndex = complement & 2047;
AIt correctly calculates the hash index without error
BIt may cause incorrect indexing if complement is negative
CIt causes a syntax error due to missing parentheses
DIt causes a runtime division by zero error
Attempts:
2 left
💡 Hint
Consider what happens if complement is negative when using bitwise AND.
🚀 Application
advanced
2:00remaining
Result of Two Sum with No Valid Pair
What will the output be when running the Two Sum classic hash solution on the array [1, 2, 3, 4] with target 8?
DSA C
#include <stdio.h>
#include <stdlib.h>

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int* hash = (int*)calloc(2048, sizeof(int));
    int* result = (int*)malloc(2 * sizeof(int));
    for (int i = 0; i < 2048; i++) hash[i] = -1;
    for (int i = 0; i < numsSize; i++) {
        int complement = target - nums[i];
        int hashIndex = complement & 2047;
        if (hash[hashIndex] != -1 && nums[hash[hashIndex]] == complement) {
            result[0] = hash[hashIndex];
            result[1] = i;
            *returnSize = 2;
            free(hash);
            return result;
        }
        hash[nums[i] & 2047] = i;
    }
    *returnSize = 0;
    free(hash);
    free(result);
    return NULL;
}

int main() {
    int nums[] = {1, 2, 3, 4};
    int target = 8;
    int returnSize = 0;
    int* indices = twoSum(nums, 4, target, &returnSize);
    if (indices != NULL) {
        printf("%d -> %d -> null\n", nums[indices[0]], nums[indices[1]]);
        free(indices);
    } else {
        printf("No solution found\n");
    }
    return 0;
}
ANo solution found
B1 -> 7 -> null
C3 -> 5 -> null
D4 -> 4 -> null
Attempts:
2 left
💡 Hint
Check if any two numbers in the array add up to 8.
Predict Output
expert
2:30remaining
Output of Two Sum with Duplicate Numbers
What is the output of this Two Sum classic hash solution when the input array contains duplicates?
DSA C
#include <stdio.h>
#include <stdlib.h>

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int* hash = (int*)calloc(2048, sizeof(int));
    int* result = (int*)malloc(2 * sizeof(int));
    for (int i = 0; i < 2048; i++) hash[i] = -1;
    for (int i = 0; i < numsSize; i++) {
        int complement = target - nums[i];
        int hashIndex = complement & 2047;
        if (hash[hashIndex] != -1 && nums[hash[hashIndex]] == complement) {
            result[0] = hash[hashIndex];
            result[1] = i;
            *returnSize = 2;
            free(hash);
            return result;
        }
        hash[nums[i] & 2047] = i;
    }
    *returnSize = 0;
    free(hash);
    free(result);
    return NULL;
}

int main() {
    int nums[] = {3, 3, 4, 5};
    int target = 6;
    int returnSize = 0;
    int* indices = twoSum(nums, 4, target, &returnSize);
    if (indices != NULL) {
        printf("%d -> %d -> null\n", nums[indices[0]], nums[indices[1]]);
        free(indices);
    } else {
        printf("No solution found\n");
    }
    return 0;
}
A3 -> 3 -> null
B4 -> 2 -> null
C5 -> 1 -> null
DNo solution found
Attempts:
2 left
💡 Hint
Look for two numbers that add up to 6, including duplicates.