Challenge - 5 Problems
Container With Most Water Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Two Pointer Approach for Container With Most Water
What is the output of the following C code that finds the maximum water container area using two pointers?
DSA C
int maxArea(int* height, int heightSize) { int left = 0, right = heightSize - 1; int max_area = 0; while (left < right) { int width = right - left; int min_height = height[left] < height[right] ? height[left] : height[right]; int area = width * min_height; 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]); int result = maxArea(height, size); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Think about the maximum area formed between two lines by moving pointers inward.
✗ Incorrect
The maximum area is formed between height[1] = 8 and height[8] = 7 with width 7, so area = 7 * 7 = 49.
❓ Predict Output
intermediate2:00remaining
Output of Brute Force Approach for Container With Most Water
What is the output of this brute force C code that calculates the maximum water container area?
DSA C
int maxArea(int* height, int heightSize) { int max_area = 0; for (int i = 0; i < heightSize; i++) { for (int j = i + 1; j < heightSize; j++) { int width = j - i; int min_height = height[i] < height[j] ? height[i] : height[j]; int area = width * min_height; if (area > max_area) max_area = area; } } return max_area; } int main() { int height[] = {1,1}; int size = sizeof(height)/sizeof(height[0]); int result = maxArea(height, size); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check the area between the only two lines.
✗ Incorrect
There are only two lines both of height 1 and distance 1 apart, so area = 1 * 1 = 1.
🔧 Debug
advanced2:00remaining
Identify the Error in Two Pointer Container Code
What error will this C code produce when compiled and run?
DSA C
int maxArea(int* height, int heightSize) { int left = 0, right = heightSize - 1; int max_area = 0; while (left < right) { int width = right - left; int min_height; if (height[left] < height[right]) min_height = height[left]; else min_height = height[right]; int area = width * min_height; 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]); int result = maxArea(height, size); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Check variable initialization and syntax carefully.
✗ Incorrect
The code is syntactically correct and logically implements the two pointer approach correctly. It outputs 49.
🧠 Conceptual
advanced2:00remaining
Why Two Pointer Approach is Efficient for Container With Most Water?
Why does the two pointer approach work efficiently to find the maximum water container area?
Attempts:
2 left
💡 Hint
Think about how moving pointers affects the possible area.
✗ Incorrect
The two pointer approach moves inward from the widest container, discarding the smaller height pointer to find larger areas efficiently without checking all pairs.
🚀 Application
expert2:00remaining
Maximum Water Container Area for Large Input
Given an array of 1,000,000 heights where all heights are 1 except the first and last which are 10, what is the maximum water container area?
Attempts:
2 left
💡 Hint
Consider the width and the minimum height between the first and last lines.
✗ Incorrect
The maximum area is between the first and last lines: width = 999,999 and min height = 10, so area = 999,999 * 10 = 9,999,990.
