Minimize Commute
Reported by candidates from Databricks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Databricks reported this one in August 2026, and the grid size is the first thing to check. Up to 100 by 100 means 10,000 cells. That's tiny per search, so the real question is how many searches you run. The answer is four, one per transport mode, each a plain BFS from S to D through only that mode's cells. No Dijkstra, no state explosion. If you blank on the setup during the OA, StealthCoder is the quiet safety net running on your screen. But this is a four-BFS problem and you can own it.
The problem
You are given a city grid containing one S, one D, blocked cells X, and transport cells 1 through 4 for Walk, Bike, Car, and Train. Choose exactly one transport mode. You may move one cell at a time in the four cardinal directions and may enter only S, D, and cells labeled with the chosen mode. Blocked cells and cells for other modes are impassable. The arrays time and cost give per-block values in the order Walk, Bike, Car, Train. For every reachable mode, use its shortest path from S to D. Return the mode with minimum total time; break a total-time tie by minimum total cost. Function minimizeCommute(grid: String[][], time: int[], cost: int[]) → String Examples Example 1 grid = [["3","3","S","2","X","X"],["3","1","1","2","X","2"],["3","1","1","2","2","2"],["3","1","1","1","D","3"],["3","3","3","3","3","4"],["4","4","4","4","4","4"]] time = [3, 2, 1, 1] cost = [0, 1, 3, 2] return = "Bike" Bike reaches D in five blocks for total time 5 × 2 = 10. Walk needs five blocks for time 15, Car needs eleven blocks for time 11, and Train is unreachable, so Bike is fastest. Constraints 1 ≤ rows, cols ≤ 100 Grid contains exactly one 'S' and one 'D' time.length == cost.length == 4 At least one mode can reach 'D'
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that choosing one mode means the weights are uniform within a run. Every step costs the same, so shortest path is just fewest blocks. Run BFS four times, once per mode, allowing only S, D, and cells labeled with that mode. If D is reached, you get a block count n. Total time is n * time[mode], total cost is n * cost[mode]. Compare by time first, then cost. The common pitfall is running one combined search that lets you switch modes mid-route, which the problem forbids. Another is forgetting unreachable modes and treating them as zero, so keep an infinity sentinel or a reached flag. Also return the mode name string (Walk, Bike, Car, Train), not the index. Complexity is O(4 * rows * cols), trivially fine for 10,000 cells. StealthCoder is your hedge if the live OA clock gets loud, but the logic fits in about 30 lines.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimize Commute cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Databricks's OA.
Databricks reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimize Commute FAQ
What's the trick in Minimize Commute?+
Run a separate BFS per mode. Within one mode every step costs the same, so the fewest blocks is the best path. Multiply the block count by that mode's time and cost, then compare. Don't mix modes in a single search, since the problem says choose exactly one.
Do I need Dijkstra for this Databricks OA?+
No. Dijkstra is for varying edge weights. Here, once a mode is fixed, every move has an identical weight, so plain BFS gives the shortest path. Dijkstra would work but adds a heap and code for no gain.
How do I handle ties between modes?+
Compare total time first. If two modes have equal total time, pick the lower total cost. A tuple comparison of (totalTime, totalCost) handles it cleanly. Keep a best tuple and update it whenever a reachable mode beats it.
What edge cases break solutions?+
Unreachable modes are the big one. Skip them rather than counting zero blocks. Also S and D are walkable for every mode, so don't block them. Check the 1 by 1 and tiny grids, and make sure you return the name string, not a number.
How do I prepare for this in 48 hours?+
Write grid BFS from scratch twice: a queue, a visited array, four direction offsets, and a distance count. Then add a mode filter to the neighbor check. That's the whole problem. Practice returning the right string label and the tie-break logic.