PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Databricks

Find the Best Commute Mode

Last updated: May 23, 2026

Quick Overview

This question evaluates competency in grid-based pathfinding, graph traversal, and multi-criteria optimization of path time and cost within the Coding & Algorithms domain.

  • medium
  • Databricks
  • Coding & Algorithms
  • Software Engineer

Find the Best Commute Mode

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a 2D grid representing a city commute map. The grid contains exactly one start cell `S` and exactly one destination cell `D`. Each other cell is one of: - `1`, `2`, `3`, or `4`: a road usable only by that commute mode. - `X`: blocked and cannot be used by any commute mode. There are four commute modes, numbered `1` through `4`. For each mode `i`, you are also given: - `time[i]`: time required to move one step using mode `i`. - `cost[i]`: cost required to move one step using mode `i`. A commute mode may move up, down, left, or right. Mode `i` may move through cells labeled `i`, and may also enter `S` and `D`. It cannot move through cells labeled with other mode numbers or through blocked cells. For each commute mode that can reach `D` from `S`, compute: - total time = number of steps in its shortest path multiplied by `time[i]` - total cost = number of steps in its shortest path multiplied by `cost[i]` Return the commute mode that has the smallest total time. If multiple modes have the same total time, return the one with the smallest total cost. If there is still a tie, return the smallest mode number. If no commute mode can reach the destination, return `-1`.

Quick Answer: This question evaluates competency in grid-based pathfinding, graph traversal, and multi-criteria optimization of path time and cost within the Coding & Algorithms domain.

Related Interview Questions

  • Design an n x n Tic-Tac-Toe Game - Databricks (medium)
  • IPv4 CIDR Range Membership Queries - Databricks (medium)
  • Optimal Commute: Nearest Transit Distance in a City Grid - Databricks (medium)
  • Choose the Best Travel Mode - Databricks (medium)
  • Implement an Alternating Tic-Tac-Toe Game - Databricks (hard)
|Home/Coding & Algorithms/Databricks

Find the Best Commute Mode

Databricks logo
Databricks
May 17, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
3
0
Practice Read
Loading...

You are given a 2D grid representing a city commute map. The grid contains exactly one start cell S and exactly one destination cell D.

Each other cell is one of:

  • 1 , 2 , 3 , or 4 : a road usable only by that commute mode.
  • X : blocked and cannot be used by any commute mode.

There are four commute modes, numbered 1 through 4. For each mode i, you are also given:

  • time[i] : time required to move one step using mode i .
  • cost[i] : cost required to move one step using mode i .

A commute mode may move up, down, left, or right. Mode i may move through cells labeled i, and may also enter S and D. It cannot move through cells labeled with other mode numbers or through blocked cells.

For each commute mode that can reach D from S, compute:

  • total time = number of steps in its shortest path multiplied by time[i]
  • total cost = number of steps in its shortest path multiplied by cost[i]

Return the commute mode that has the smallest total time. If multiple modes have the same total time, return the one with the smallest total cost. If there is still a tie, return the smallest mode number. If no commute mode can reach the destination, return -1.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Databricks•More Software Engineer•Databricks Software Engineer•Databricks Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.