Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current index can jump to or beyond the last index.
DSA C++
if (i + nums[i] [1] n - 1) return true;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' causes wrong logic.
Using '==' misses jumps that go beyond the last index.
✗ Incorrect
We check if the current position plus its jump length reaches or passes the last index, so '>=' is correct.
2fill in blank
mediumComplete the code to update the farthest reachable index from the current position.
DSA C++
farthest = std::max(farthest, i [1] nums[i]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' subtracts jump length, which is incorrect.
Using '*' or '/' does not represent jump distance.
✗ Incorrect
We add the current index and its jump length to find the farthest reachable index.
3fill in blank
hardFix the error in the loop condition to iterate over all indices correctly.
DSA C++
for (int i = 0; i [1] n; ++i) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bounds access.
Using '>' or '>=' prevents the loop from running.
✗ Incorrect
The loop should run while i is less than n to cover all indices from 0 to n-1.
4fill in blank
hardFill both blanks to complete the condition that checks if the current index is reachable.
DSA C++
if (i [1] farthest) { farthest = std::max(farthest, i [2] nums[i]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' in the first blank skips reachable indices.
Using '<' in the second blank causes wrong calculation.
✗ Incorrect
We check if i is less than or equal to farthest to ensure it's reachable, then update farthest with max of current farthest and i + nums[i].
5fill in blank
hardFill all three blanks to complete the function that returns true if the last index is reachable.
DSA C++
bool canJump(std::vector<int>& nums) {
int n = nums.size();
int farthest = 0;
for (int i = 0; i [1] n; ++i) {
if (i [2] farthest) {
farthest = std::max(farthest, i [3] nums[i]);
} else {
return false;
}
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' in the loop condition causes infinite loops.
Using '>' in reachability check skips valid indices.
Using '-' instead of '+' miscalculates farthest.
✗ Incorrect
The loop runs while i < n; we check if i <= farthest to confirm reachability; then update farthest with i + nums[i].
