Challenge - 5 Problems
Container With Most Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Two Pointer Approach for Container With Most Water
What is the output of the following TypeScript code that finds the maximum water container area?
DSA Typescript
function maxArea(height: number[]): number {
let left = 0;
let right = height.length - 1;
let maxArea = 0;
while (left < right) {
const width = right - left;
const currentHeight = Math.min(height[left], height[right]);
const currentArea = width * currentHeight;
maxArea = Math.max(maxArea, currentArea);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
console.log(maxArea([1,8,6,2,5,4,8,3,7]));Attempts:
2 left
š” Hint
Think about the widest container with the shortest height limiting the water.
ā Incorrect
The maximum area is 49, formed between the lines at index 1 (height 8) and index 8 (height 7), width 7, area 7*7=49.
š§ Conceptual
intermediateWhy Two Pointer Approach Works for Container With Most Water
Why does the two pointer approach correctly find the maximum water container area?
Attempts:
2 left
š” Hint
Consider which pointer limits the current area and why moving it might help.
ā Incorrect
The smaller height limits the area, so moving that pointer might find a taller line and increase area. Moving the larger height pointer won't help increase area.
š§ Debug
advancedIdentify the Error in Container With Most Water Implementation
What error does the following TypeScript code produce when run?
DSA Typescript
function maxArea(height: number[]): number {
let left = 0;
let right = height.length - 1;
let maxArea = 0;
while (left <= right) {
const width = right - left;
const currentHeight = Math.min(height[left], height[right]);
const currentArea = width * currentHeight;
maxArea = Math.max(maxArea, currentArea);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
console.log(maxArea([1,8,6,2,5,4,8,3,7]));Attempts:
2 left
š” Hint
Check the loop condition and simulate the execution with the given input.
ā Incorrect
The code returns 49 correctly. The loop condition 'left <= right' leads to an extra iteration when left == right (width=0, area=0), but maxArea is unaffected since Math.max ignores 0. No negative width occurs as the loop terminates properly.
ā Predict Output
advancedOutput of Modified Container With Most Water Code with Early Return
What is the output of this TypeScript code that attempts to find max water container but returns early?
DSA Typescript
function maxArea(height: number[]): number {
let left = 0;
let right = height.length - 1;
let maxArea = 0;
while (left < right) {
const width = right - left;
const currentHeight = Math.min(height[left], height[right]);
const currentArea = width * currentHeight;
maxArea = Math.max(maxArea, currentArea);
if (height[left] < height[right]) {
return maxArea;
} else {
right--;
}
}
return maxArea;
}
console.log(maxArea([1,8,6,2,5,4,8,3,7]));Attempts:
2 left
š” Hint
Look at when the function returns inside the loop.
ā Incorrect
The function returns immediately after first comparison if height[left] < height[right], so it returns area between index 0 and 8: width 8, height 1, area 8.
š Application
expertMaximum Water Container Area for Large Input
Given an array of 100,000 elements where height[i] = i % 1000 + 1, what is the maximum water container area?
Attempts:
2 left
š” Hint
The maximum height is 1000 at indices i where i % 1000 === 999. Find the maximum distance between such indices.
ā Incorrect
Positions of height 1000: 999, 1999, ..., 99999. The widest pair is index 999 and 99999, width = 99999 - 999 = 99000, area = 99000 * 1000 = 99000000.
