Bird
Raised Fist0
DSA C#programming~20 mins

Container With Most Water in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Container With Most Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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));
    }
}
A48
B56
C49
D36
Attempts:
2 left
💡 Hint
Think about the two pointers moving inward and the area calculation at each step.
🧠 Conceptual
intermediate
1:30remaining
Understanding Two Pointer Movement
In the Container With Most Water problem, why do we move the pointer pointing to the shorter line inward?
ABecause the problem requires moving the left pointer first
BBecause moving the shorter line pointer might find a taller line and increase area
CBecause moving any pointer randomly is inefficient
DBecause moving the taller line pointer cannot increase the area
Attempts:
2 left
💡 Hint
Consider how the area depends on the shorter line height.
🔧 Debug
advanced
2:00remaining
Identify 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));
    }
}
ANo error, outputs 49
BInfinite loop
CIndexOutOfRangeException
DCompilation error
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
🚀 Application
advanced
1:30remaining
Maximum Water Container with Custom Heights
Given the heights array {4,3,2,1,4}, what is the maximum water container area?
A6
B12
C8
D16
Attempts:
2 left
💡 Hint
Check pairs of lines with maximum width and minimum height.
❓ Predict Output
expert
2:30remaining
Output 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));
    }
}
A
left=0, right=6, area=6
left=0, right=5, area=5
left=0, right=4, area=4
left=1, right=4, area=9
left=2, right=4, area=6
left=3, right=4, area=25
Max Area: 25
B
left=0, right=6, area=6
left=1, right=6, area=15
left=2, right=6, area=10
left=3, right=6, area=15
left=4, right=6, area=10
left=5, right=6, area=5
Max Area: 15
C
left=0, right=6, area=6
left=0, right=5, area=5
left=0, right=4, area=4
left=1, right=4, area=9
left=2, right=4, area=6
left=3, right=4, area=25
left=3, right=3, area=0
Max Area: 25
D
left=0, right=6, area=6
left=1, right=6, area=18
left=2, right=6, area=12
left=3, right=6, area=15
left=4, right=6, area=10
Max Area: 18
Attempts:
2 left
💡 Hint
Trace pointer moves and area calculations carefully.

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

  1. 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.
  2. Step 2: Identify what to calculate

    We calculate the area between two lines as the width times the smaller height.
  3. Final Answer:

    The maximum area of water that can be contained between two lines. -> Option A
  4. 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

  1. Step 1: Recall C# main method syntax

    The main method must be public static void and accept string array args.
  2. Step 2: Match options with correct syntax

    public static void Main(string[] args) matches the correct signature: public static void Main(string[] args).
  3. Final Answer:

    public static void Main(string[] args) -> Option B
  4. 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

  1. Step 1: Understand the two-pointer approach

    Start with left=0 and right=8, calculate area and move pointers inward choosing smaller height side.
  2. Step 2: Trace key iterations

    Max area found is 49 between height[1]=8 and height[8]=7 with width 7.
  3. Step 3: Continue tracing

    However, a larger area of 56 is found between height[1]=8 and height[6]=8 with width 7.
  4. Final Answer:

    56 -> Option C
  5. 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

  1. Step 1: Check loop condition

    Using 'left <= right' causes area calculation when left == right, width zero, unnecessary.
  2. Step 2: Correct loop condition

    Change to 'left < right' to avoid zero width calculation.
  3. Final Answer:

    The loop condition should be 'left < right' instead of 'left <= right'. -> Option D
  4. 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

  1. Step 1: Use two-pointer approach with tracking

    While moving pointers, keep track of the largest and second largest areas found.
  2. 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.
  3. Final Answer:

    Track the top two max areas during the pointer moves and return the second largest. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard