Interview Questions/Coding/Quarantine Hopper Route

Quarantine Hopper Route

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Quarantine Hopper Route

A courier robot starts at checkpoint 0 on a one-dimensional quarantine road. It wants to reach checkpoint home. In one move, the robot may jump forward by exactly forward units or jump backward by exactly backward units. However, because backward movement destabilizes the robot, it is not allowed to make two backward jumps in a row. Some checkpoints are sealed and cannot be landed on. Input Format: You are given an array blocked and three integers forward, backward, and home. Output Format: Return the minimum number of jumps needed to reach home, or -1 if no valid route exists.

Examples

Example 1
Input:
blocked = [14, 4, 18, 1, 15]
forward = 3
backward = 15
home = 9
Output:
3

Explanation: A shortest valid route is 0 -> 3 -> 6 -> 9. It uses 3 hops, matching the output 3.

Approach hint

Look for the state that actually matters after each operation.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp