Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the left pointer at the start of the array.
DSA Java
int left = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting the left pointer at 1 instead of 0.
Using array.length which is out of bounds.
✗ Incorrect
The left pointer should start at index 0, which is the first element of the array.
2fill in blank
mediumComplete the code to initialize the right pointer at the end of the array.
DSA Java
int right = [1] - 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using array.length() which is a method for strings, not arrays.
Using array.size which is not valid for arrays.
✗ Incorrect
The right pointer should start at the last index, which is array.length - 1.
3fill in blank
hardFix the error in the while loop condition to continue while left is less than right.
DSA Java
while (left [1] right) { // logic here }
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= or <= which causes the loop to run too long or miss cases.
Using > which never runs if left starts less than right.
✗ Incorrect
The loop should run while left is less than right to avoid overlapping pointers.
4fill in blank
hardFill both blanks to return the correct pointer indices inside the loop.
DSA Java
if (array[left] + array[right] == target) { return new int[] { [1], [2] }; } else if (array[left] + array[right] < target) { left++; } else { right--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the target or array instead of indices.
Swapping left and right in the return array.
✗ Incorrect
Return the indices of the left and right pointers when their sum equals the target.
5fill in blank
hardFill all three blanks to create a map of numbers to their indices for a one-pass hashmap approach.
DSA Java
Map<Integer, Integer> map = new HashMap<>(); for (int [1] = 0; [1] < array.length; [1]++) { int complement = [2] - array[[1]]; if (map.containsKey(complement)) { return new int[] { map.get(complement), [1] }; } map.put(array[[1]], [1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' or 'sum' instead of 'i' for the loop variable.
Using 'sum' instead of 'target' for the complement calculation.
✗ Incorrect
Use 'i' as the loop variable and index, and 'target' as the value to find complements.
