Find best downhill ski run from a start
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates algorithmic problem-solving in grid graph traversal and dynamic programming, requiring computation of the maximum-length strictly decreasing path from a start cell plus analysis of time and space complexity.
Part 1: Maximum Downhill Score for One Skier
Constraints
- 0 <= R, C
- If the grid is non-empty, all rows have the same length
- If the grid is non-empty, 1 <= R * C <= 100000
- -10^9 <= grid[r][c] <= 10^9
- If the grid is non-empty, the given start cell is within bounds
Examples
Input: ([[9, 6, 3], [8, 7, 2], [5, 4, 1]], 0, 0)
Expected Output: 7
Explanation: One optimal run is 9 -> 8 -> 7 -> 6 -> 3 -> 2 -> 1, which visits 7 cells.
Input: ([[5, 5], [5, 5]], 0, 0)
Expected Output: 1
Explanation: No move is allowed because all neighboring cells have equal elevation, not lower.
Input: ([[42]], 0, 0)
Expected Output: 1
Explanation: A single-cell grid always has score 1 because the start cell counts.
Input: ([], 0, 0)
Expected Output: 0
Explanation: An empty grid has no cells to visit.
Hints
- Think of each cell as a node in a graph. Because you can only move to a strictly lower elevation, cycles are impossible.
- Use DFS with memoization so that the best downhill score from each cell is computed only once.
Part 2: Efficient Downhill Scores for Many Skiers
Constraints
- 0 <= R, C
- If the grid is non-empty, all rows have the same length
- If the grid is non-empty, 1 <= R * C <= 100000
- 0 <= len(starts) <= 100000
- -10^9 <= grid[r][c] <= 10^9
- If the grid is non-empty, each start cell in starts is within bounds
Examples
Input: ([[9, 6, 3], [8, 7, 2], [5, 4, 1]], [(0, 0), (1, 1), (2, 2)])
Expected Output: [7, 5, 1]
Explanation: The scores are 7 from 9, 5 from 7, and 1 from 1.
Input: ([[5, 5], [5, 5]], [(0, 0), (1, 1)])
Expected Output: [1, 1]
Explanation: No skier can move because no neighboring cell has lower elevation.
Input: ([[3, 2], [4, 1]], [])
Expected Output: []
Explanation: No starting cells were requested, so the result is an empty list.
Input: ([], [(0, 0), (2, 2)])
Expected Output: [0, 0]
Explanation: With an empty grid, every requested score is 0.
Hints
- The answer for a starting cell depends only on that cell, not on which skier asks for it.
- Memoize the best downhill score from each cell once, then answer each query by reusing that stored value.