📖 Scenario: You are analyzing daily stock price changes represented as an array of numbers. Each number shows the daily change factor (positive for gain, negative for loss). You want to find the maximum product of a contiguous subarray to understand the best consecutive days for investment growth.
🎯 Goal: Build a TypeScript program that finds the maximum product of any contiguous subarray within a given array of numbers.
📋 What You'll Learn
Create an array called nums with the exact values [2, 3, -2, 4].
Create two variables called maxProd and minProd and set both to the first element of nums.
Use a for loop with variable i starting from 1 to iterate over nums.
Inside the loop, update maxProd and minProd to track the maximum and minimum products ending at the current index.
Create a variable called result and set it to the first element of nums.
Update result inside the loop to keep the maximum product found so far.
Print the value of result at the end.
💡 Why This Matters
🌍 Real World
Finding the maximum product subarray helps in financial analysis to identify the best consecutive days for investment growth based on daily change factors.
💼 Career
This problem is common in technical interviews and helps develop skills in dynamic programming and array manipulation.
Progress0 / 4 steps
1
Create the input array
Create an array called nums with the exact values [2, 3, -2, 4].
DSA Typescript
Hint
Use const nums = [2, 3, -2, 4]; to create the array.
2
Initialize maxProd, minProd, and result
Create two variables called maxProd and minProd and set both to the first element of nums. Also create a variable called result and set it to the first element of nums.
DSA Typescript
Hint
Set maxProd, minProd, and result all equal to nums[0].
3
Loop through the array and update maxProd and minProd
Use a for loop with variable i starting from 1 to iterate over nums. Inside the loop, create a variable tempMax to store the current maxProd. Update maxProd to the maximum of nums[i], maxProd * nums[i], and minProd * nums[i]. Update minProd to the minimum of nums[i], tempMax * nums[i], and minProd * nums[i]. Then update result to the maximum of result and maxProd.
DSA Typescript
Hint
Use a for loop starting at 1. Use Math.max and Math.min to update maxProd and minProd. Remember to save the old maxProd in tempMax before updating.
4
Print the maximum product result
Print the value of result using console.log(result).
DSA Typescript
Hint
Use console.log(result) to print the maximum product.
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 sum of its elements
B. The contiguous subarray with the largest product of its elements
C. The longest subarray with all positive numbers
D. The subarray with the smallest product of its elements
Solution
Step 1: Understand the problem statement
The problem asks for a contiguous part of the array where the product of all numbers is as large as possible.
Step 2: Differentiate from similar problems
This is different from finding the largest sum or smallest product; it specifically focuses on the product of elements in a continuous segment.
Final Answer:
The contiguous subarray with the largest product of its elements -> Option B
Quick Check:
Maximum product subarray means largest product segment [OK]
Hint: Look for continuous elements with max product, not sum [OK]
Common Mistakes:
Confusing product with sum
Ignoring that subarray must be contiguous
Thinking about longest subarray instead of max product
2. Which of the following TypeScript function signatures correctly represents a function to find the maximum product subarray?
easy
A. function maxProduct(nums: number[]): number
B. function maxProduct(nums: string): number
C. function maxProduct(nums: number): number[]
D. function maxProduct(nums: number[]): string
Solution
Step 1: Check input parameter type
The input should be an array of numbers, so nums: number[] is correct.
Step 2: Check return type
The function returns the maximum product value, which is a number, not an array or string.
Final Answer:
function maxProduct(nums: number[]): number -> Option A
Quick Check:
Input array, output number matches signature [OK]
Hint: Input is number array, output is single number [OK]
Common Mistakes:
Using string type for input or output
Returning array instead of number
Incorrect parameter types
3. What is the output of the following TypeScript code?
function maxProduct(nums: number[]): number {
let maxProd = nums[0];
let minProd = nums[0];
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
[maxProd, minProd] = [minProd, maxProd];
}
maxProd = Math.max(nums[i], maxProd * nums[i]);
minProd = Math.min(nums[i], minProd * nums[i]);
result = Math.max(result, maxProd);
}
return result;
}
console.log(maxProduct([2, 3, -2, 4]));
medium
A. 6
B. 4
C. -2
D. 24
Solution
Step 1: Initialize variables and start loop
maxProd, minProd, and result start at 2. Loop from index 1 to 3.
Step 2: Iterate and update maxProd, minProd, and result
Hint: Track max and min products, swap on negative [OK]
Common Mistakes:
Not swapping max and min on negative number
Only tracking max product, ignoring min product
Confusing max product with sum
4. Identify the bug in this TypeScript function for maximum product subarray:
function maxProduct(nums: number[]): number {
let maxProd = 1;
let minProd = 1;
let result = Number.MIN_VALUE;
for (let num of nums) {
if (num < 0) {
let temp = maxProd;
maxProd = minProd;
minProd = temp;
}
maxProd = Math.max(num, maxProd * num);
minProd = Math.min(num, minProd * num);
result = Math.max(result, maxProd);
}
return result;
}
medium
A. Initializing maxProd and minProd to 0 instead of 1
B. Not swapping maxProd and minProd when num is positive
C. Using a for loop instead of forEach
D. Using Number.MIN_VALUE instead of Number.NEGATIVE_INFINITY for result
Solution
Step 1: Understand Number.MIN_VALUE usage
Number.MIN_VALUE is the smallest positive number close to zero, not the most negative number.
Step 2: Correct initialization for result
To track maximum product correctly, result should start at Number.NEGATIVE_INFINITY to handle negative products properly.
Final Answer:
Using Number.MIN_VALUE instead of Number.NEGATIVE_INFINITY for result -> Option D
Quick Check:
Use Number.NEGATIVE_INFINITY for max tracking [OK]
Hint: Use Number.NEGATIVE_INFINITY for max initial value [OK]
Common Mistakes:
Confusing Number.MIN_VALUE with negative infinity
Not swapping max and min on negative number
Wrong initial values for maxProd and minProd
5. Given the array [-1, -3, -10, 0, 60], what is the maximum product subarray and its product?