Choose the Best Travel Mode
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: Solve a grid-based pathfinding problem where a traveler must choose the transportation mode with the lowest travel time and cost tie-breakers. The question evaluates BFS, reachability under mode-specific passability rules, tie-breaking logic, and efficient handling of multiple traversal modes.
Constraints
- 1 <= number of rows, columns <= 200
- start and destination are valid grid coordinates.
- Each mode key is a one-character string that may appear in the grid.
- Each mode value is a pair `(time_per_step, cost_per_step)` of positive integers.
- The traveler must use exactly one mode for the entire trip.
Examples
Input: (['SAA', '..A', 'BBD'], (0, 0), (2, 2), {'A': (2, 5), 'B': (1, 10)})
Expected Output: 'B'
Explanation: Mode B has lower total time.
Input: (['S..D'], (0, 0), (0, 3), {'A': (1, 5), 'B': (1, 2)})
Expected Output: 'B'
Explanation: Time ties, so cost breaks the tie.
Input: (['SBXD', '.B..'], (0, 0), (0, 3), {'A': (1, 1), 'B': (1, 1)})
Expected Output: 'B'
Explanation: Only mode B can pass through the B-only cells around the blocker.
Input: (['SAXD'], (0, 0), (0, 3), {'A': (1, 1), 'B': (1, 1)})
Expected Output: None
Explanation: The only row is blocked before the destination.
Input: (['S.D'], (0, 0), (0, 2), {'A': (1, 1), 'B': (1, 1)})
Expected Output: 'A'
Explanation: Time and cost tie, so the lexicographically smallest mode wins.
Input: (['S.C', 'XXC', '..D'], (0, 0), (2, 2), {'A': (1, 1), 'C': (3, 1)})
Expected Output: 'C'
Explanation: Mode C is the only mode with a path through the C cells.
Hints
- For a fixed mode, this is a shortest-path problem on an unweighted grid, so BFS gives the fewest steps.
- After BFS finds the number of steps for a mode, multiply by that mode's time and cost, then apply the tie-breakers.
- Shared road cells can be traversed by every mode; cells labeled with another mode cannot.