ORIGINAL CODING QUESTION
Fewest leaps through a course ladder
Course modules stand in a row from 0 to n-1. Module i lets you advance to any module from i+1 to i+reach[i]; a reach of 0 means you cannot move forward from it. Starting at module 0, return the fewest moves needed to reach module n-1, or -1 if it cannot be reached. With a single module the answer is 0.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n reach values.
Constraints
1 ≤ n ≤ 200; 0 ≤ reach ≤ 200.
Example 1
Input
5 2 3 1 1 4
Output
2
Move 0→1, then 1→4: two moves.
Example 2
Input
5 3 2 1 0 4
Output
-1
Every route stalls at module 3, which has reach 0.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Group modules by the number of moves needed to reach them.
Hint 2
All modules reachable with j moves form a contiguous block starting after the previous block.
Hint 3
Track the furthest module reachable from the current block; if it does not grow, the end is unreachable.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →