Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the maximum area variable initialized to zero.
DSA C++
int maxArea = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxArea to 1 or a negative number.
Using INT_MAX which is too large and incorrect here.
✗ Incorrect
We start with maxArea as 0 because initially we haven't calculated any area.
2fill in blank
mediumComplete the code to calculate the width between two pointers left and right.
DSA C++
int width = [1] - left;Attempts:
3 left
💡 Hint
Common Mistakes
Using left - right which can be negative.
Using maxArea or min which are unrelated here.
✗ Incorrect
Width is the distance between the right and left pointers, so right - left.
3fill in blank
hardFix the error in the code to update maxArea with the maximum of current maxArea and new area.
DSA C++
maxArea = std::[1](maxArea, area);Attempts:
3 left
💡 Hint
Common Mistakes
Using std::min which gives the smaller value.
Using std::abs or std::swap which are unrelated.
✗ Incorrect
We use std::max to keep the larger value between maxArea and area.
4fill in blank
hardFill both blanks to move the pointer pointing to the smaller height inward.
DSA C++
if (height[left] < height[right]) { left [1]; } else { right [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving both pointers in the same direction.
Using += or -= without a value.
✗ Incorrect
If left height is smaller, move left pointer forward (++). Otherwise, move right pointer backward (--).
5fill in blank
hardFill all three blanks to complete the function that returns the maximum water container area.
DSA C++
int maxAreaContainer(const std::vector<int>& height) {
int left = 0, right = height.size() [1] 1;
int maxArea = 0;
while (left < right) {
int width = right [2] left;
int area = std::min(height[left], height[right]) * width;
maxArea = std::max(maxArea, area);
if (height[left] < height[right]) {
left [3];
} else {
right--;
}
}
return maxArea;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting right to size + 1 or size.
Using + instead of - for width calculation.
Using -- instead of ++ for left pointer increment.
✗ Incorrect
We set right to size - 1, calculate width as right - left, and increment left pointer when left height is smaller.
