Count Routes
Updated
Problem statement
At the Robotics Innovation Campus "UBERBOT", the robot IG-0R climbs a vertical metal wall. The wall is a grid with n rows (levels) and m columns. Each cell is 'X', a magnetic contact point (a node) the robot can use, or '#', empty space. The first row of grid is the top level (level 1) and the last row is the bottom level (level n).
A route is a list of nodes the robot visits in order. A route is valid when all of these hold:
- The first node is on the bottom level and the last node is on the top level.
- The robot only moves up or sideways along a level. It never goes down.
- The robot uses at least one node and at most two nodes on every level. So it cannot skip a level, and each time it goes up it lands on the level directly above.
- Each node is used at most once. The two nodes on a level can be used in either order.
- Every jump from one node to the next has a straight-line (Euclidean) distance of at most
w. The distance between nodes(i1, j1)and(i2, j2)issqrt((i1 - i2)2 + (j1 - j2)2).
Count the valid routes. Two routes are different if they visit different nodes or visit them in a different order. A route may stop after its first or second node on the top level. Because the count can be very large, return it modulo 998244353.
Complete the function countRoutes in the editor.
countRoutes has the following parameters:
char[][] grid: the wall, top level first, where each cell is'X'or'#'int w: the farthest the robot can jump
The function returns an int: the number of valid routes, modulo 998244353.
Function
countRoutes(grid: char[][], w: int) → intExamples
Example 1
grid = [["X", "X", "#", "X"], ["#", "X", "X", "#"], ["#", "X", "#", "X"]]w = 1return = 2The wall has 3 levels and 4 columns, and w = 1. With w = 1 a jump can only go to a node straight above or right next to the robot on the same level. Columns are counted from 1 below.
The bottom level has nodes in columns 2 and 4. From column 4 there is no node above and no neighbor, so that start is stuck. From column 2 the robot goes up to column 2 on the middle level. A sideways step to column 3 leads nowhere, because the node above it is empty. So the robot goes up to column 2 on the top level. There it can stop, or step sideways to column 1 and stop. That gives 2 routes:
- bottom column 2, middle column 2, top column 2
- bottom column 2, middle column 2, top column 2, top column 1
The answer is 2.
Constraints
2 ≤ n ≤ 2000, wherenisgrid.length1 ≤ m ≤ 2000, wheremis the length of every row1 ≤ w ≤ 2000- Each cell of
gridis'X'or'#'.
More Uber interview questions
- Random Bingo Card GeneratorONSITE INTERVIEW · Seen Dec 2026
- Travelers per Consecutive City TripletONSITE INTERVIEW · Seen Dec 2026
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Largest Consistent Visiting GroupAuxoAI · OA
- Profitable Currency Exchange CycleOptiver · OAOptiver interview questions
- Maximum Strength of Every NeuronMicrosoft · OAMicrosoft interview questions
- Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
- Tree Node With Maximum BeautyC3.ai · OAC3.ai interview questions
- Longest Anchored Song ChainIntuit · OAIntuit interview questions
- Cheapest Flight with Its PathAirbnb · OA · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Cheapest Visiting Order with PrerequisitesIndeed · OAIndeed interview questions
- Cities on Shortest Delivery RoutesMathWorks · OAMathWorks interview questions