Solve Grid Path and Graph Sampling
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
This interview note described two coding problems:
1. **Grid path with shortest time and lowest cost**
You are given an `m x n` grid containing a start cell `S`, an end cell `E`, blocked cells, and open cells. You may move up, down, left, or right to adjacent open cells. Each move takes exactly 1 unit of time. Some cells also have a non-negative fee that is paid when you enter that cell. Find a path from `S` to `E` that minimizes total travel time; among all shortest-time paths, choose one with the minimum total fee. Return the minimum time and minimum fee, or report that no path exists.
2. **Uniformly sample edges that connect multiple components**
You are given `n` disjoint connected undirected graphs `G1, G2, ..., Gn`. You may add edges only between vertices belonging to different graphs, and any such cross-graph edge is allowed. Write a function that returns a random set of exactly `n - 1` new edges such that, after adding them, the union of all `n` graphs becomes connected. The returned edge set must be sampled **uniformly** from the set of all valid connecting edge sets of size `n - 1`.
Quick Answer: This question evaluates graph algorithms and probabilistic/combinatorial reasoning, covering multi-criteria shortest-path optimization (minimizing travel time with a secondary fee objective) and uniform random sampling of edge sets that connect disjoint components.