Following problem was asked to me in Amazon Screenign round. Sharing hoping that it will help someone else.
A delivery truck is tasked with delivering a package to a home on a street. Think of the street as a number line with the home located at position x. Your goal is to navigate the truck to the target location.
Truck Movement Rules:
- The truck can move forward exactly
apositions (to the right) or move backward exactlybpositions (to the left). - The truck cannot move backward twice in a row.
- The truck cannot move to any forbidden positions due to parking restrictions.
- The truck can move beyond position
xto the right but cannot move left past the origin (position 0).
Objective:
Return true if the truck can reach the target home within n steps. Otherwise, return false if there is no possible path or it cannot arrive within n steps.
Input:
forbidden: List of integers representing positions that are restricted.a: A positive integer representing the number of positions the truck moves forward.b: A positive integer representing the number of positions the truck moves backward.x: A positive integer representing the target home position.n: A positive integer representing the maximum number of steps allowed.
Examples:
Example 1:
- Input: forbidden = [5, 7, 9], a = 4, b = 3, x = 10, n = 6
- Output: true
- Explanation: 3 steps forward (0 -> 4 -> 8 -> 10) will arrive at the target home.
Example 2:
- Input: forbidden = [6, 8, 12], a = 5, b = 2, x = 11, n = 5
- Output: false
Example 3:
- Input: forbidden = [1, 3, 5, 7], a = 6, b = 4, x = 8, n = 3
- Output: true
- Explanation: One step forward (0 -> 6) then one step backward (6 -> 2) will arrive at the target home.
Comments (2)


