Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the last index is reachable from the first index.
DSA C#
public bool CanJump(int[] nums) {
int maxReach = 0;
for (int i = 0; i <= maxReach; i++) {
maxReach = Math.Max(maxReach, i [1] nums[i]);
if (maxReach >= nums.Length - 1) return true;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication or division which doesn't represent jump distance.
✗ Incorrect
We add the current index i to nums[i] to find the farthest reachable index from i.
2fill in blank
mediumComplete the code to iterate through the array while the current index is within the maximum reachable index.
DSA C#
public bool CanJump(int[] nums) {
int maxReach = 0;
int i = 0;
while (i [1] maxReach && maxReach < nums.Length - 1) {
maxReach = Math.Max(maxReach, i + nums[i]);
i++;
}
return maxReach >= nums.Length - 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < which prevents the loop from entering initially.
Using > or >= which reverses the logic.
✗ Incorrect
We continue while i <= maxReach to ensure we process all reachable positions including the boundary.
3fill in blank
hardFix the error in the code that updates the maximum reachable index.
DSA C#
public bool CanJump(int[] nums) {
int maxReach = 0;
for (int i = 0; i <= maxReach; i++) {
maxReach = Math.Max(maxReach, [1] + nums[i]);
if (maxReach >= nums.Length - 1) return true;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using maxReach instead of i causes incorrect calculation.
Using nums.Length or i + 1 which are unrelated to current position.
✗ Incorrect
We add the current index i to nums[i] to find the farthest reachable index.
4fill in blank
hardFill both blanks to create a loop that iterates while the index is within the maximum reachable range and updates maxReach correctly.
DSA C#
public bool CanJump(int[] nums) {
int maxReach = 0;
int i = 0;
while (i [1] maxReach && maxReach [2] nums.Length - 1) {
maxReach = Math.Max(maxReach, i + nums[i]);
i++;
}
return maxReach >= nums.Length - 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < in the first blank skips boundary positions.
Using >= or > in the second blank reverses or breaks the loop logic.
✗ Incorrect
The loop continues while i <= maxReach and maxReach < nums.Length - 1.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each index to its maximum reachable index if the jump length is greater than zero.
DSA C#
var maxReachMap = new Dictionary<int, int>(); for (int i = 0; i < nums.Length; i++) { if (nums[i] [1] 0) { maxReachMap[[2]] = [3] + nums[i]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= instead of > allows zero jumps which are not useful.
Using wrong variable names for keys or values.
✗ Incorrect
We check if nums[i] is greater than 0, then map index i to i + nums[i].
