Bird
Raised Fist0
DSA Cprogramming~10 mins

Container With Most Water in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Container With Most Water
Start with two pointers
↓
Calculate area between pointers
↓
Update max area if larger
↓
Move pointer at smaller height inward
↓
Pointers meet?
No→Repeat calculation
Yes↓
Return max area
We use two pointers at the ends, calculate area, move the smaller pointer inward, and repeat until pointers meet.
Execution Sample
DSA C
int maxArea(int* height, int heightSize) {
    int left = 0, right = heightSize - 1;
    int max_area = 0;
    while (left < right) {
        int area = (right - left) * (height[left] < height[right] ? height[left] : height[right]);
        if (area > max_area) max_area = area;
        if (height[left] < height[right]) left++; else right--;
    }
    return max_area;
}
Calculates the maximum water container area by moving two pointers inward based on height comparison.
Execution Table
StepOperationLeft PointerRight PointerHeight[left]Height[right]Area CalculatedMax AreaPointer MovedVisual State
1Initialize pointers0813min(1,3)*8=88Move left (height[left]<height[right])[1,8,6,2,5,4,8,7,3]
2Calculate area1883min(8,3)*7=2121Move right (height[left]>=height[right])[1,8,6,2,5,4,8,7,3]
3Calculate area1787min(8,7)*6=3636Move right (height[left]>=height[right])[1,8,6,2,5,4,8,7,3]
4Calculate area1688min(8,8)*5=4040Move right (height[left]>=height[right])[1,8,6,2,5,4,8,7,3]
5Calculate area1584min(8,4)*4=1640Move right (height[left]>=height[right])[1,8,6,2,5,4,8,7,3]
6Calculate area1485min(8,5)*3=1540Move right (height[left]>=height[right])[1,8,6,2,5,4,8,7,3]
7Calculate area1382min(8,2)*2=440Move right (height[left]>=height[right])[1,8,6,2,5,4,8,7,3]
8Calculate area1286min(8,6)*1=640Move right (height[left]>=height[right])[1,8,6,2,5,4,8,7,3]
9Pointers meet1188-40Stop[1,8,6,2,5,4,8,7,3]
💡 Pointers meet at index 1, loop ends with max area 40
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8Final
left0111111111
right8876543211
max_area082136404040404040
Key Moments - 3 Insights
Why do we move the pointer at the smaller height instead of the larger one?
Because the area is limited by the smaller height, moving the smaller pointer inward may find a taller line and potentially increase area, as shown in steps 1-8 in the execution_table.
Why does the loop stop when left equals right?
When left equals right, the pointers meet, meaning no width remains between them, so no container can be formed. This is shown in step 9 of the execution_table.
How is the area calculated at each step?
Area is calculated as the width between pointers times the smaller height of the two pointers, as detailed in the 'Area Calculated' column of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the area calculated?
A36
B21
C35
D8
💡 Hint
Check the 'Area Calculated' column at step 3 in the execution_table.
At which step does the max area first update to 40?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Max Area' column in the execution_table to see when it changes to 40.
If the height at index 0 was 10 instead of 1, what would happen at step 1?
AArea would be larger than 8
BArea would be smaller than 8
CArea would remain 8
DPointer moved would be right instead of left
💡 Hint
Area is width times smaller height; increasing height[0] from 1 to 10 increases the smaller height at step 1.
Concept Snapshot
Container With Most Water:
- Use two pointers at array ends
- Calculate area = width * min(height[left], height[right])
- Move pointer at smaller height inward
- Update max area if current area is larger
- Stop when pointers meet
- Returns max water container area
Full Transcript
This concept uses two pointers starting at the ends of the height array. At each step, it calculates the area formed by the lines at these pointers, which is the width between pointers times the smaller height. It updates the maximum area found so far. Then, it moves the pointer at the smaller height inward to try to find a taller line that could increase the area. This repeats until the pointers meet, meaning no more containers can be formed. The maximum area found is returned. The execution table shows each step's pointers, heights, area calculation, and pointer movement. The variable tracker follows the pointers and max area changes. Key moments clarify why the smaller pointer moves and why the loop stops. The visual quiz tests understanding of these steps.

Practice

(1/5)
1. What does the problem Container With Most Water ask you to find given an array of heights?
easy
A. The number of lines taller than a given height
B. The minimum height in the array
C. The sum of all heights in the array
D. The maximum area formed between two lines representing container walls

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 maximize

    We want to maximize the area between two lines, which is the width between them times the smaller height.
  3. Final Answer:

    The maximum area formed between two lines representing container walls -> Option D
  4. Quick Check:

    Max area = max water container [OK]
Hint: Focus on max area between two heights, not sum or count [OK]
Common Mistakes:
  • Confusing max area with sum of heights
  • Looking for minimum height only
  • Counting lines instead of calculating area
2. Which of the following is the correct way to declare the two-pointer variables for solving Container With Most Water in C?
easy
A. int left = 1, right = n;
B. int left = n, right = 0;
C. int left = 0, right = n - 1;
D. int left = -1, right = n + 1;

Solution

  1. Step 1: Understand pointer initialization

    We start with left pointer at the start (index 0) and right pointer at the end (index n-1) of the array.
  2. Step 2: Check each option

    int left = 0, right = n - 1; correctly initializes left=0 and right=n-1. Others use invalid or out-of-bound indices.
  3. Final Answer:

    int left = 0, right = n - 1; -> Option C
  4. Quick Check:

    Two pointers at array ends = int left = 0, right = n - 1; [OK]
Hint: Start pointers at array ends: 0 and n-1 [OK]
Common Mistakes:
  • Starting pointers out of array bounds
  • Using 1-based indexing in C arrays
  • Swapping left and right initialization
3. What is the output of the following C code snippet for the array height = {1, 8, 6, 2, 5, 4, 8, 3, 7}?
int maxArea(int* height, int heightSize) {
    int left = 0, right = heightSize - 1, max_area = 0;
    while (left < right) {
        int area = (right - left) * (height[left] < height[right] ? height[left] : height[right]);
        if (area > max_area) max_area = area;
        if (height[left] < height[right]) left++; else right--;
    }
    return max_area;
}

int main() {
    int height[] = {1,8,6,2,5,4,8,3,7};
    int size = sizeof(height)/sizeof(height[0]);
    printf("%d", maxArea(height, size));
    return 0;
}
medium
A. 72
B. 49
C. 64
D. 56

Solution

  1. Step 1: Initialize pointers and max_area

    left=0, right=8, max_area=0. Calculate area each step and update max_area.
  2. Step 2: Trace iterations

    left=0 (1), right=8 (7), area=8*1=8, max=8, left++; left=1 (8), right=8 (7), area=7*7=49, max=49; next areas smaller (e.g. 6*3=18, 5*8=40), max_area remains 49.
  3. Final Answer:

    49 -> Option B
  4. Quick Check:

    Max water container area = 49 [OK]
Hint: Move pointer with smaller height to find max area fast [OK]
Common Mistakes:
  • Calculating area incorrectly as sum instead of width*height
  • Not updating max_area properly
  • Moving wrong pointer in the loop
4. Identify the bug in this C code snippet for Container With Most Water and select the fix:
int maxArea(int* height, int heightSize) {
    int left = 0, right = heightSize - 1, max_area = 0;
    while (left <= right) {
        int area = (right - left) * (height[left] < height[right] ? height[left] : height[right]);
        if (area > max_area) max_area = area;
        if (height[left] < height[right]) left++; else right--;
    }
    return max_area;
}
medium
A. Change while condition to left < right
B. Initialize max_area to -1
C. Use max_area += area instead of max_area = area
D. Swap left and right pointers initialization

Solution

  1. Step 1: Analyze loop condition

    The loop uses while(left <= right), which allows left == right, causing zero width and unnecessary calculation.
  2. Step 2: Correct loop condition

    Change condition to while(left < right) to avoid zero width and infinite loop risk.
  3. Final Answer:

    Change while condition to left < right -> Option A
  4. Quick Check:

    Loop must run while left < right [OK]
Hint: Use strict less than for pointers to avoid zero width [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Incorrectly updating max_area by adding
  • Changing pointer initialization unnecessarily
5. Given an array of heights representing vertical lines, how would you modify the two-pointer approach to find the container with the most water if the container must include the line at index 0?
hard
A. Fix left pointer at 0 and move right pointer from end towards left
B. Fix right pointer at 0 and move left pointer from start towards right
C. Use a nested loop to check all pairs including index 0
D. Use binary search to find max area including index 0

Solution

  1. Step 1: Understand the constraint

    The container must include the line at index 0, so left pointer stays fixed at 0.
  2. Step 2: Move right pointer from end to left

    Move right pointer from the last index towards 1, calculating area with fixed left pointer to find max area.
  3. Final Answer:

    Fix left pointer at 0 and move right pointer from end towards left -> Option A
  4. Quick Check:

    Fix one pointer at 0, move other to find max area [OK]
Hint: Fix one pointer, move the other to maximize area [OK]
Common Mistakes:
  • Moving left pointer instead of fixing it
  • Using nested loops unnecessarily
  • Applying binary search incorrectly
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