Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if we can jump from the first position.
DSA Javascript
function canJump(nums) {
let maxReach = 0;
for (let i = 0; i <= [1]; i++) {
if (i > maxReach) return false;
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using nums.length instead of nums.length - 1 causes out-of-bound errors.
Looping too far or too short misses checking the last position.
✗ Incorrect
We loop until the last index, which is nums.length - 1, to check if we can reach the end.
2fill in blank
mediumComplete the code to update the maximum reachable index.
DSA Javascript
function canJump(nums) {
let maxReach = 0;
for (let i = 0; i <= nums.length - 1; i++) {
if (i > maxReach) return false;
maxReach = Math.max(maxReach, i [1] nums[i]);
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition changes the logic.
Forgetting to update maxReach properly causes wrong results.
✗ Incorrect
We add i and nums[i] to find the farthest index reachable from position i.
3fill in blank
hardFix the error in the loop condition to avoid out-of-bounds errors.
DSA Javascript
function canJump(nums) {
let maxReach = 0;
for (let i = 0; i [1] nums.length; i++) {
if (i > maxReach) return false;
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index out of range errors.
Using '>' or '>=' reverses the loop logic.
✗ Incorrect
The loop should run while i is less than nums.length to avoid going out of bounds.
4fill in blank
hardFill both blanks to return false when the current index is unreachable and to update maxReach correctly.
DSA Javascript
function canJump(nums) {
let maxReach = 0;
for (let i = 0; i < nums.length; i++) {
if (i [1] maxReach) return false;
maxReach = Math.max(maxReach, i [2] nums[i]);
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the if condition reverses the logic.
Using subtraction instead of addition to update maxReach.
✗ Incorrect
If current index i is greater than maxReach, we cannot reach it, so return false. Update maxReach by adding i and nums[i].
5fill in blank
hardFill all three blanks to create a function that returns true if the last index is reachable, using a loop and maxReach variable.
DSA Javascript
function canJump(nums) {
let maxReach = 0;
for (let i = 0; i [1] nums.length; i++) {
if (i [2] maxReach) return false;
maxReach = Math.max(maxReach, i [3] nums[i]);
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' instead of '<' in loop condition causes errors.
Using '<' instead of '>' in if condition reverses logic.
Using '-' instead of '+' to update maxReach.
✗ Incorrect
Loop while i is less than nums.length. If i is greater than maxReach, return false. Update maxReach by adding i and nums[i].
