Bird
0
0
DSA Cprogramming~20 mins

Maximum Product Subarray in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Product Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Maximum Product Subarray Calculation
What is the output of the following C code that finds the maximum product of a contiguous subarray?
DSA C
int maxProduct(int* nums, int numsSize) {
    int max_so_far = nums[0];
    int min_so_far = nums[0];
    int result = nums[0];
    for (int i = 1; i < numsSize; i++) {
        if (nums[i] < 0) {
            int temp = max_so_far;
            max_so_far = min_so_far;
            min_so_far = temp;
        }
        max_so_far = nums[i] > max_so_far * nums[i] ? nums[i] : max_so_far * nums[i];
        min_so_far = nums[i] < min_so_far * nums[i] ? nums[i] : min_so_far * nums[i];
        if (max_so_far > result) result = max_so_far;
    }
    return result;
}

int main() {
    int arr[] = {2, 3, -2, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    int max_product = maxProduct(arr, size);
    printf("%d\n", max_product);
    return 0;
}
A-2
B4
C8
D6
Attempts:
2 left
💡 Hint
Track max and min products at each step, especially when encountering negative numbers.
Predict Output
intermediate
2:00remaining
Result of Maximum Product Subarray with Negative Numbers
What will be printed by this C program that calculates the maximum product subarray?
DSA C
int maxProduct(int* nums, int numsSize) {
    int max_so_far = nums[0];
    int min_so_far = nums[0];
    int result = nums[0];
    for (int i = 1; i < numsSize; i++) {
        if (nums[i] < 0) {
            int temp = max_so_far;
            max_so_far = min_so_far;
            min_so_far = temp;
        }
        max_so_far = nums[i] > max_so_far * nums[i] ? nums[i] : max_so_far * nums[i];
        min_so_far = nums[i] < min_so_far * nums[i] ? nums[i] : min_so_far * nums[i];
        if (max_so_far > result) result = max_so_far;
    }
    return result;
}

int main() {
    int arr[] = {-2, 0, -1};
    int size = sizeof(arr) / sizeof(arr[0]);
    int max_product = maxProduct(arr, size);
    printf("%d\n", max_product);
    return 0;
}
A-2
B-1
C0
D2
Attempts:
2 left
💡 Hint
Zero resets the product, consider subarrays including zero.
🧠 Conceptual
advanced
2:00remaining
Understanding the Role of min_so_far in Maximum Product Subarray
Why does the algorithm keep track of the minimum product so far (min_so_far) when finding the maximum product subarray?
ABecause minimum product helps to find the smallest subarray length.
BBecause the minimum product can become maximum if multiplied by a negative number.
CBecause minimum product stores the smallest element in the array.
DBecause minimum product is used to count negative numbers in the array.
Attempts:
2 left
💡 Hint
Think about how negative numbers affect multiplication.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Maximum Product Subarray Code
What error will this C code produce when run, and why?
DSA C
int maxProduct(int* nums, int numsSize) {
    int max_so_far = nums[0];
    int min_so_far = nums[0];
    int result = nums[0];
    for (int i = 1; i <= numsSize; i++) {
        if (nums[i] < 0) {
            int temp = max_so_far;
            max_so_far = min_so_far;
            min_so_far = temp;
        }
        max_so_far = nums[i] > max_so_far * nums[i] ? nums[i] : max_so_far * nums[i];
        min_so_far = nums[i] < min_so_far * nums[i] ? nums[i] : min_so_far * nums[i];
        if (max_so_far > result) result = max_so_far;
    }
    return result;
}

int main() {
    int arr[] = {2, 3, -2, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    int max_product = maxProduct(arr, size);
    printf("%d\n", max_product);
    return 0;
}
ARuntime error due to accessing out-of-bounds array index.
BSyntax error because of missing semicolon.
CReturns incorrect maximum product 4.
DCompiles and runs correctly, output 6.
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
🚀 Application
expert
3:00remaining
Maximum Product Subarray with Zero and Negative Numbers
Given the array [1, -2, -3, 0, 7, -8, -2], what is the maximum product of any contiguous subarray?
A112
B48
C0
D14
Attempts:
2 left
💡 Hint
Consider subarrays before and after zero, and how negatives multiply.