Solve Sliding Window and Grid Search
Company: Bytedance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string algorithms and constrained shortest-path/search problems, covering sliding-window techniques for bounded-distinct substrings and grid pathfinding with stateful fuel, recharge mechanics, and cost accumulation.
Part 1: Longest Substring With At Most K Distinct Characters
Constraints
- 0 <= len(s) <= 200000
- 0 <= k <= len(s)
- s may contain any printable characters
Examples
Input: ('eceba', 2)
Expected Output:
Explanation: The longest valid substring is 'ece', which has length 3.
Input: ('aa', 1)
Expected Output:
Explanation: The whole string contains only 1 distinct character.
Input: ('abaccc', 2)
Expected Output:
Explanation: The longest valid substring is 'accc'.
Input: ('', 3)
Expected Output:
Explanation: An empty string has no substrings, so the answer is 0.
Input: ('abc', 0)
Expected Output:
Explanation: With k = 0, no non-empty substring is allowed.
Hints
- Try maintaining a sliding window [left, right] that always satisfies the constraint.
- Use a frequency map so you can quickly tell when the window has more than k distinct characters.
Part 2: Minimum-Cost Path in a Grid With Fuel and Recharge Cells
Constraints
- 1 <= m, n <= 30
- 0 <= cost[i][j] <= 10^6
- 0 <= K <= 60
- blocked, recharge, and cost have the same dimensions
- The start and destination cells are not blocked
- All costs are non-negative integers
Examples
Input: ([[1, 2, 3], [4, 1, 1], [4, 2, 1]], [[False, False, False], [False, False, False], [False, False, False]], [[False, False, False], [False, False, False], [False, False, False]], 4)
Expected Output:
Explanation: One minimum-cost path is (0,0) -> (0,1) -> (1,1) -> (1,2) -> (2,2), with total cost 1 + 2 + 1 + 1 + 1 = 6.
Input: ([[1, 1, 1, 1]], [[False, False, False, False]], [[False, False, True, False]], 2)
Expected Output:
Explanation: The recharge cell allows the trip to continue: total cost is 1 + 1 + 1 + 1 = 4.
Input: ([[1, 1], [1, 1]], [[False, False], [False, False]], [[False, False], [False, False]], 1)
Expected Output:
Explanation: The destination is 2 moves away, but only 1 unit of fuel is available and there is no recharge.
Input: ([[7]], [[False]], [[True]], 0)
Expected Output:
Explanation: Start and destination are the same cell, so only the starting cell's cost is counted.
Hints
- A state is not just a cell; the remaining fuel matters too.
- Because all costs are non-negative, Dijkstra's algorithm over states (row, col, fuel) is a good fit.
Part 3: Minimum-Cost Grid Path When K Is Very Large
Constraints
- 1 <= m, n <= 60
- 0 <= cost[i][j] <= 10^6
- 0 <= K <= 10^9
- blocked, recharge, and cost have the same dimensions
- The start and destination cells are not blocked
- All costs are non-negative integers
Examples
Input: ([[1, 100, 100], [1, 100, 1], [1, 1, 1]], [[False, False, False], [False, False, False], [False, False, False]], [[False, False, False], [False, False, False], [False, False, False]], 1000000000)
Expected Output:
Explanation: Fuel is effectively unlimited for this grid, so the answer is the cheapest weighted path: down, down, right, right.
Input: ([[1, 1], [1, 1]], [[False, True], [True, False]], [[False, False], [False, False]], 1000000000)
Expected Output:
Explanation: The destination is disconnected by blocked cells.
Input: ([[9]], [[False]], [[False]], 0)
Expected Output:
Explanation: Single-cell grid: the start is already the destination.
Input: ([[1, 1, 1, 1]], [[False, False, False, False]], [[False, False, True, False]], 2)
Expected Output:
Explanation: Even in the optimized version, moderate K values must still be handled correctly; the recharge cell makes the trip possible.
Hints
- Between two refuels, an optimal path never needs to revisit a cell when all costs are non-negative.
- That means the useful fuel level can be capped by the number of traversable cells minus 1. If fuel can already cover any simple path, you can ignore fuel entirely and run ordinary Dijkstra on the grid.