Implement a Coin-Constrained Jump Strategy
CoinbaseSoftware Engineer · Online AssessmentLast updated
Design a pathfinding algorithm to reach a target coordinate in the minimum number of jumps subject to physics-based velocity adjustments, obstacle avoidance, and a non-negative coin balance constraint.
Problem Description
An autonomous rover is navigating a one-dimensional coordinate track starting at position 0 with an initial speed of 0 and a budget of initial_coins. The goal is to reach position target in the minimum number of jumps without letting the coin balance drop below 0 at any point.
Motion Rules
- At each step, if the rover's current speed is
v, it chooses a new speedv'such that:max(1, v - 1) <= v' <= min(max_speed, v + 1)(Note: From the starting state wherev = 0, the first jump must always selectv' = 1.) - The rover leaps forward to
new_position = current_position + v'. - The rover cannot jump past
target(i.e.,new_position <= target). - The rover cannot land on any position listed in
obstacles.
Coin Mechanics
- Track positions may have coin modifiers given by a dictionary
coin_changesmapping coordinatepto integerdelta:- A positive
deltaadds coins upon landing. - A negative
deltarepresents a toll or hazard that subtracts coins upon landing. - Positions not present in
coin_changeshave a delta of0.
- A positive
- Upon landing on
new_position, the rover's coins update:coins = coins + coin_changes.get(new_position, 0). - If
coins = 0). Iftargetcannot be reached, return-1.
Function Signature
def min_jumps_to_target(
target: int,
initial_coins: int,
obstacles: list[int],
coin_changes: dict[int, int],
max_speed: int
) -> int:
pass
Examples
Example 1
Input:
target = 10
initial_coins = 5
obstacles = []
coin_changes = {6: -10}
max_speed = 4
Output: 5
Explanation:
The quickest physical path in distance would take 4 jumps: 0 -> 1 (v=1) -> 3 (v=2) -> 6 (v=3) -> 10 (v=4). However, landing on position 6 deducts 10 coins, dropping the balance to 5 - 10 = -5 1 (v=1) -> 2 (v=1) -> 4 (v=2) -> 7 (v=3) -> 10 (v=3), entirely bypassing position 6 and keeping coins = 5 >= 0.
Example 2
Input:
target = 4
initial_coins = 0
obstacles = [2]
coin_changes = {}
max_speed = 3
Output: -1
Explanation:
From position = 0, v = 0, the only valid first move is v' = 1 landing on position 1. From position 1 with v = 1, the possible next speeds are 1 (landing on 2, which is an obstacle) or 2 (landing on 3). From position 3 with v = 2, the next speed must be at least 1, landing at >= 4. A speed of 1 lands on 4 (target). Total jumps: 0 -> 1 -> 3 -> 4 (3 jumps).
(Note: If position 3 were also blocked, it would return -1.)
Constraints
1 <= target <= 10000 <= initial_coins <= 10001 <= max_speed <= 500 <= len(obstacles) <= target- All positions in
obstaclessatisfy1 <= pos < target. coin_changesmaps positions1 <= pos <= targetto integers between-1000and1000.0is never an obstacle and has no coin modifier.
Hints
Hint 1
Model the state as (position, velocity), noting that position is strictly increasing since velocity is always at least 1.
Hint 2
A standard BFS level-by-level exploration yields the minimum number of jumps, but you must account for coin balances at identical (position, velocity) states.
Hint 3
Maintain the maximum coin balance seen so far for each (position, velocity) pair; prune a state if you reach it with less than or equal coins compared to a previously explored state.
Follow-up questions
- How would you modify the state representation and search if the rover were allowed to reverse direction (negative velocities)?
- If the goal changed from minimizing jumps to maximizing final coins at the target, what algorithm would you choose and what is its time complexity?
- How would you handle dynamic obstacles that appear or disappear based on the current step number?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.