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 Container With Most Water Calculation
What is the output of the following C# code that calculates the maximum water container area?
DSA C#
using System; class Program { static int MaxArea(int[] height) { int maxArea = 0, left = 0, right = height.Length - 1; while (left < right) { int width = right - left; int area = Math.Min(height[left], height[right]) * width; if (area > maxArea) maxArea = area; if (height[left] < height[right]) left++; else right--; } return maxArea; } static void Main() { int[] heights = {1,8,6,2,5,4,8,3,7}; Console.WriteLine(MaxArea(heights)); } }
Attempts:
2 left
💡 Hint
Think about the two pointers moving inward and the area calculation at each step.
✗ Incorrect
The maximum area is found between the lines at index 1 (height 8) and index 8 (height 7), width 7, area = 7 * 7 = 49.
🧠 Conceptual
intermediateUnderstanding Two Pointer Movement
In the Container With Most Water problem, why do we move the pointer pointing to the shorter line inward?
Attempts:
2 left
💡 Hint
Consider how the area depends on the shorter line height.
✗ Incorrect
The area is limited by the shorter line. Moving the shorter line pointer inward may find a taller line and increase area, while moving the taller line pointer won't help.
🔧 Debug
advancedIdentify the Error in Container With Most Water Code
What error does the following C# code produce when run?
DSA C#
using System; class Program { static int MaxArea(int[] height) { int maxArea = 0, left = 0, right = height.Length - 1; while (left <= right) { int width = right - left; int area = Math.Min(height[left], height[right]) * width; if (area > maxArea) maxArea = area; if (height[left] < height[right]) left++; else right--; } return maxArea; } static void Main() { int[] heights = {1,8,6,2,5,4,8,3,7}; Console.WriteLine(MaxArea(heights)); } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
No runtime error; the code outputs the correct 49. The <= condition performs an extra iteration when left == right (area=0), but terminates normally without issues.
🚀 Application
advancedMaximum Water Container with Custom Heights
Given the heights array {4,3,2,1,4}, what is the maximum water container area?
Attempts:
2 left
💡 Hint
Check pairs of lines with maximum width and minimum height.
✗ Incorrect
The max area is between index 0 and 4: min(4,4) * (4 - 0) = 4 * 4 = 16.
❓ Predict Output
expertOutput of Modified Container With Most Water Code with Debug Prints
What is the output of the following C# code including debug prints?
DSA C#
using System; class Program { static int MaxArea(int[] height) { int maxArea = 0, left = 0, right = height.Length - 1; while (left < right) { int width = right - left; int area = Math.Min(height[left], height[right]) * width; Console.WriteLine($"left={left}, right={right}, area={area}"); if (area > maxArea) maxArea = area; if (height[left] < height[right]) left++; else right--; } return maxArea; } static void Main() { int[] heights = {1,3,3,25,25,1,1}; Console.WriteLine("Max Area: " + MaxArea(heights)); } }
Attempts:
2 left
💡 Hint
Trace pointer moves and area calculations carefully.
✗ Incorrect
The pointers move inward choosing the smaller height pointer each time. The last iteration is when left=3 and right=4, then loop ends. No iteration with left=3, right=3 occurs.
