Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum area variable.
DSA Javascript
let maxArea = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxArea to 1 or Infinity causes incorrect comparisons later.
✗ Incorrect
We start with maxArea as 0 because initially we have no container formed.
2fill in blank
mediumComplete the code to calculate the width between two pointers.
DSA Javascript
let width = [1] - left;Attempts:
3 left
💡 Hint
Common Mistakes
Using left - right instead of right - left results in negative width.
✗ Incorrect
Width is the distance between the right and left pointers.
3fill in blank
hardFix the error in the condition to update maxArea with the larger area.
DSA Javascript
if (currentArea [1] maxArea) { maxArea = currentArea; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' causes maxArea to never update correctly.
✗ Incorrect
We update maxArea only if currentArea is greater than maxArea.
4fill in blank
hardFill both blanks to move the pointers correctly based on height comparison.
DSA Javascript
if (height[left] [1] height[right]) { left [2] 1; } else { right -= 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' in comparison reverses pointer movement logic.
Using '-=' to move left pointer causes infinite loop.
✗ Incorrect
If left height is less, move left pointer right by 1; else move right pointer left by 1.
5fill in blank
hardFill all three blanks to complete the function that returns the maximum water container area.
DSA Javascript
function maxArea(height) {
let left = 0, right = height.length - 1;
let maxArea = 0;
while (left < right) {
let width = right - left;
let currentArea = Math.min(height[left], height[right]) [1] width;
if (currentArea [2] maxArea) {
maxArea = currentArea;
}
if (height[left] [3] height[right]) {
left += 1;
} else {
right -= 1;
}
}
return maxArea;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' for area calculation.
Using '<' instead of '>' in maxArea update condition.
Using '>' instead of '<' in height comparison.
✗ Incorrect
Multiply min height by width for area; update maxArea if currentArea is greater; move left pointer if left height is less.
