Bird
Raised Fist0
DSA Cprogramming~20 mins

Maximum Product Subarray in DSA C - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the Maximum Product Subarray problem ask you to find in an array of integers?
easy
A. The subarray with the largest difference between elements
B. The contiguous subarray with the largest sum of its elements
C. The subarray with the smallest product of its elements
D. The contiguous subarray with the largest product of its elements

Solution

  1. Step 1: Understand the problem and differentiate from similar problems

    The problem asks for a contiguous part of the array where the product of all elements is the highest possible. Unlike maximum sum subarray, this problem focuses on product, which can be affected by negative numbers and zeros.
  2. Final Answer:

    The contiguous subarray with the largest product of its elements -> Option D
  3. Quick Check:

    Maximum product subarray = largest product subarray [OK]
Hint: Focus on product, not sum, for this problem [OK]
Common Mistakes:
  • Confusing product with sum
  • Ignoring negative numbers impact
  • Thinking subarray can be non-contiguous
2. Which of the following is the correct way to declare variables to track the maximum and minimum products at each step in C for the Maximum Product Subarray problem?
easy
A. int maxProd = 1, minProd = 1;
B. int maxProd, minProd = 0;
C. int maxProd = 0, minProd = 0;
D. int maxProd = -1, minProd = -1;

Solution

  1. Step 1: Initialize maxProd and minProd properly and check alternatives

    Both maxProd and minProd should start at 1 because multiplying by 1 does not change the product. Other initializations use zero (nullifies products), negative values, or leave variables uninitialized leading to incorrect calculations or undefined behavior.
  2. Final Answer:

    int maxProd = 1, minProd = 1; -> Option A
  3. Quick Check:

    Initialize maxProd and minProd to 1 [OK]
Hint: Start max and min product as 1 to avoid zero multiplication [OK]
Common Mistakes:
  • Initializing maxProd or minProd to 0
  • Not initializing both variables
  • Using negative initial values
3. What is the output of the following C code snippet for the array int arr[] = {2, 3, -2, 4};?
int maxProductSubarray(int* nums, int numsSize) {
    int maxProd = nums[0], minProd = nums[0], result = nums[0];
    for (int i = 1; i < numsSize; i++) {
        if (nums[i] < 0) {
            int temp = maxProd;
            maxProd = minProd;
            minProd = temp;
        }
        maxProd = (nums[i] > maxProd * nums[i]) ? nums[i] : maxProd * nums[i];
        minProd = (nums[i] < minProd * nums[i]) ? nums[i] : minProd * nums[i];
        if (maxProd > result) result = maxProd;
    }
    return result;
}

int main() {
    int arr[] = {2, 3, -2, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d", maxProductSubarray(arr, size));
    return 0;
}
medium
A. 4
B. 6
C. -2
D. 24

Solution

  1. Step 1: Trace the loop and determine final result

    Start with maxProd=2, minProd=2, result=2. - i=1 (3): maxProd=max(3,2*3)=6, minProd=min(3,2*3)=3, result=6 - i=2 (-2): swap to maxProd=3, minProd=6; maxProd=max(-2,3*(-2))=-2, minProd=min(-2,6*(-2))=-12, result=6 - i=3 (4): maxProd=max(4,-2*4)=4, minProd=min(4,-12*4)=-48, result=6 The maximum product found is 6 from subarray [2,3].
  2. Final Answer:

    6 -> Option B
  3. Quick Check:

    Max product subarray = 6 [OK]
Hint: Swap max and min when negative number encountered [OK]
Common Mistakes:
  • Not swapping max and min on negative number
  • Confusing max product with sum
  • Incorrectly updating result
4. Identify the bug in the following C code snippet for Maximum Product Subarray and select the correct fix:
int maxProductSubarray(int* nums, int numsSize) {
    int maxProd = 1, minProd = 1, result = nums[0];
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] < 0) {
            int temp = maxProd;
            maxProd = minProd;
            minProd = temp;
        }
        maxProd = (nums[i] > maxProd * nums[i]) ? nums[i] : maxProd * nums[i];
        minProd = (nums[i] < minProd * nums[i]) ? nums[i] : minProd * nums[i];
        if (maxProd > result) result = maxProd;
    }
    return result;
}
medium
A. Initialize maxProd and minProd with nums[0] and start loop from i=1
B. Remove the swap of maxProd and minProd when nums[i] < 0
C. Initialize result to 1 instead of nums[0]
D. Change the loop to run from i=1 to numsSize-1 without other changes

Solution

  1. Step 1: Check initialization and loop start; analyze other options

    maxProd and minProd should start as nums[0] to correctly handle the first element. Loop should start from i=1 to avoid reprocessing first element. Removing swap breaks handling of negative numbers. Initializing result to 1 is incorrect if nums[0] is negative. Changing loop start without fixing initialization causes errors.
  2. Final Answer:

    Initialize maxProd and minProd with nums[0] and start loop from i=1 -> Option A
  3. Quick Check:

    Proper initialization and loop start fix bug [OK]
Hint: Initialize with first element, loop from second element [OK]
Common Mistakes:
  • Starting loop from 0 with maxProd=1
  • Not swapping max and min on negative
  • Wrong initialization of result
5. Given the array int arr[] = {-1, -3, -10, 0, 60};, what is the maximum product of any contiguous subarray?
hard
A. 1800
B. 0
C. 60
D. -1

Solution

  1. Step 1: Analyze subarrays and their products

    Relevant products: [60]=60, [-3,-10]=30, [-1,-3]=3, [-1,-3,-10]=-30, [-10]=-10, subarrays crossing or including 0=0. No larger contiguous product exists (e.g. [-3,-10,60] skips 0, invalid).
  2. Final Answer:

    60 -> Option C
  3. Quick Check:

    Max product subarray = 60 [OK]
Hint: Check negative pairs and zeros carefully [OK]
Common Mistakes:
  • Thinking non-contiguous subarrays like [-3,-10,60] are valid
  • Choosing [-3,-10]=30 instead of 60
  • Confusing zero as max product
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard