📖 Scenario: You are helping a water storage company to find the best place to build a container between vertical lines on a flat surface. Each line's height is given, and you want to find two lines that together with the x-axis form a container that holds the most water.
🎯 Goal: Build a program that finds the maximum amount of water a container can store using the heights of vertical lines.
📋 What You'll Learn
Create an array of integers called heights with the exact values: 1, 8, 6, 2, 5, 4, 8, 3, 7
Create two integer variables called left and right to represent the start and end indices of the array
Use a while loop to move left and right pointers and calculate the maximum water container size
Print the maximum water container size as an integer
💡 Why This Matters
🌍 Real World
This problem models how to find the best container shape to hold the most water between vertical lines, useful in storage and design.
💼 Career
Understanding two-pointer techniques and array manipulation is important for coding interviews and real-world software development.
Progress0 / 4 steps
1
Create the heights array
Create an integer array called heights with these exact values: 1, 8, 6, 2, 5, 4, 8, 3, 7
DSA C#
Hint
Use int[] heights = { ... }; to create the array with the exact numbers.
2
Initialize pointers for the container edges
Create two integer variables called left and right. Set left to 0 and right to the last index of the heights array.
DSA C#
Hint
Use int left = 0; and int right = heights.Length - 1; to set the pointers.
3
Calculate the maximum water container size
Create an integer variable called maxArea and set it to 0. Use a while loop that runs while left is less than right. Inside the loop, calculate the area between left and right using the minimum height of the two lines multiplied by the distance between them. Update maxArea if the current area is larger. Move the pointer that points to the shorter line inward by one.
DSA C#
Hint
Use a while loop and update maxArea by comparing current area. Move the pointer of the smaller height.
4
Print the maximum water container size
Print the value of maxArea using Console.WriteLine.
DSA C#
Hint
Use Console.WriteLine(maxArea); to print the result.
Practice
(1/5)
1. What does the problem Container With Most Water ask you to find given an array of heights?
easy
A. The maximum area of water that can be contained between two lines.
B. The minimum height in the array.
C. The sum of all heights in the array.
D. The number of lines taller than a given height.
Solution
Step 1: Understand the problem statement
The problem asks to find two lines that together with the x-axis form a container holding the most water.
Step 2: Identify what to calculate
We calculate the area between two lines as the width times the smaller height.
Final Answer:
The maximum area of water that can be contained between two lines. -> Option A
Quick Check:
Container area = width * min(height1, height2) [OK]
Hint: Look for two lines maximizing width and height [OK]
Common Mistakes:
Thinking it's about sum of heights
Confusing maximum area with minimum height
Ignoring width between lines
2. Which of the following is the correct way to declare the main method in a C# program that solves the Container With Most Water problem?
easy
A. void main()
B. public static void Main(string[] args)
C. static Main()
D. public void Main(string args)
Solution
Step 1: Recall C# main method syntax
The main method must be public static void and accept string array args.
Step 2: Match options with correct syntax
public static void Main(string[] args) matches the correct signature: public static void Main(string[] args).
Final Answer:
public static void Main(string[] args) -> Option B
Quick Check:
Main method signature = public static void Main(string[] args) [OK]
Hint: Main method needs public static void with string[] args [OK]
Common Mistakes:
Using void main() without public static
Missing string array brackets
Incorrect parameter type
3. What is the output of the following C# code snippet?
int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
int maxArea = 0;
int left = 0, right = height.Length - 1;
while (left < right) {
int area = Math.Min(height[left], height[right]) * (right - left);
if (area > maxArea) maxArea = area;
if (height[left] < height[right]) left++; else right--;
}
Console.WriteLine(maxArea);
medium
A. 48
B. 49
C. 56
D. 64
Solution
Step 1: Understand the two-pointer approach
Start with left=0 and right=8, calculate area and move pointers inward choosing smaller height side.
Step 2: Trace key iterations
Max area found is 49 between height[1]=8 and height[8]=7 with width 7.
Step 3: Continue tracing
However, a larger area of 56 is found between height[1]=8 and height[6]=8 with width 7.
Final Answer:
56 -> Option C
Quick Check:
Max area = 56 [OK]
Hint: Use two pointers from ends, move smaller height inward [OK]
Common Mistakes:
Calculating area incorrectly
Moving wrong pointer
Not updating maxArea properly
4. Identify the bug in this C# code snippet for Container With Most Water:
int maxArea = 0;
int left = 0, right = height.Length - 1;
while (left <= right) {
int area = Math.Min(height[left], height[right]) * (right - left);
if (area > maxArea) maxArea = area;
if (height[left] < height[right]) left++;
else right--;
}
return maxArea;
medium
A. maxArea should be initialized to -1 instead of 0.
B. The area calculation should use Math.Max instead of Math.Min.
C. The pointers should both move inward every iteration.
D. The loop condition should be 'left < right' instead of 'left <= right'.
Solution
Step 1: Check loop condition
Using 'left <= right' causes area calculation when left == right, width zero, unnecessary.
Step 2: Correct loop condition
Change to 'left < right' to avoid zero width calculation.
Final Answer:
The loop condition should be 'left < right' instead of 'left <= right'. -> Option D
Quick Check:
Loop must run while left < right [OK]
Hint: Loop until pointers cross, not equal [OK]
Common Mistakes:
Using <= causes zero width area
Using Math.Max instead of Math.Min for height
Moving both pointers each time
5. Given an array of heights, how would you modify the two-pointer approach to find the container with the second largest water area?
hard
A. Track the top two max areas during the pointer moves and return the second largest.
B. Sort the array and pick the two largest heights.
C. Use a nested loop to check all pairs and pick the second largest area.
D. Return the area of the container formed by the first and last lines.
Solution
Step 1: Use two-pointer approach with tracking
While moving pointers, keep track of the largest and second largest areas found.
Step 2: Update max and second max areas properly
When a new area is larger than max, update second max to old max, max to new area; else if area is between max and second max, update second max.
Final Answer:
Track the top two max areas during the pointer moves and return the second largest. -> Option A
Quick Check:
Track max and second max areas during iteration [OK]
Hint: Keep two max values while scanning with two pointers [OK]
Common Mistakes:
Sorting loses original indices and width info
Nested loops are inefficient
Returning first and last lines area only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.