Solve BFS and grid tasks
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This collection evaluates understanding of search techniques (binary search and BFS), graph and grid traversal, state-space shortest-path reasoning, randomized placement with adjacency counting, and attention to implementation quality and complexity trade-offs.
Part 1: Highest Safe Floor
Constraints
- 0 <= len(safe) <= 200000
- The list is monotonic: all `True` values come before all `False` values
- Floor numbers are 1-based
Examples
Input: []
Expected Output: 0
Explanation: There are no floors, so the highest safe floor is 0.
Input: [True]
Expected Output: 1
Explanation: The only floor is safe.
Input: [False, False, False]
Expected Output: 0
Explanation: No floor is safe.
Input: [True, True, True, False, False]
Expected Output: 3
Explanation: Floors 1, 2, and 3 are safe, and floor 4 is the first unsafe floor.
Input: [True, True, True]
Expected Output: 3
Explanation: All floors are safe, so the threshold is the last floor.
Hints
- This is equivalent to finding the index of the last `True` value.
- If you know how to find the first `False`, you can derive the answer from that too.
Part 2: Shortest Path on a Combination Lock
Constraints
- `start` and `target` are 4-character strings of digits
- 0 <= len(deadends) <= 10000
- All blocked states are unique
Examples
Input: ('0000', '0202', ['0201', '0101', '0102', '1212', '2002'])
Expected Output: 6
Explanation: A shortest valid path reaches the target in 6 moves.
Input: ('0000', '8888', ['0001', '0010', '0100', '1000', '0009', '0090', '0900', '9000'])
Expected Output: -1
Explanation: All immediate neighbors of the start are blocked, so no move is possible.
Input: ('1234', '1234', [])
Expected Output: 0
Explanation: The start is already the target.
Input: ('0000', '9000', [])
Expected Output: 1
Explanation: Decrementing the first wheel once wraps from 0 to 9.
Input: ('0000', '0001', ['0000'])
Expected Output: -1
Explanation: The start state itself is blocked, so the search cannot begin.
Hints
- Each lock state is a node in an unweighted graph, so think BFS.
- From one state, there are exactly 8 next states: for each of 4 wheels, rotate up or down.
Part 3: Nearest Exit in a 2D Grid
Constraints
- 1 <= number of rows, columns <= 200
- `maze[entrance[0]][entrance[1]]` is `'.'`
- Moves are allowed only in the four cardinal directions
Examples
Input: ([['+','+','.','+'],['.','.','.','+'],['+','+','+','.']], (1, 2))
Expected Output: 1
Explanation: The cell `(0, 2)` is an exit and is one step away.
Input: ([['+','+','+'],['.','.','.'],['+','+','+']], (1, 0))
Expected Output: 2
Explanation: The entrance is on the boundary but does not count; the nearest other exit is `(1, 2)`.
Input: ([['.']], (0, 0))
Expected Output: -1
Explanation: The only open cell is the entrance, so there is no valid exit.
Input: ([['+','+','+'],['+','.','+'],['+','+','+']], (1, 1))
Expected Output: -1
Explanation: The entrance is completely enclosed by walls.
Hints
- Because every move costs the same, BFS gives the shortest path.
- Do not count the entrance as an exit even if it lies on the boundary.
Part 4: Seeded Minesweeper Board Generation
Constraints
- 1 <= rows, cols <= 50
- 0 <= N <= rows * cols
- Use the exact seeded shuffle described in the statement
Examples
Input: (1, 1, 0, 5)
Expected Output: [[0]]
Explanation: A 1x1 board with no mines contains a single 0.
Input: (2, 2, 4, 0)
Expected Output: [['*', '*'], ['*', '*']]
Explanation: When every cell is a mine, the whole board is filled with `'*'`.
Input: (2, 3, 2, 1)
Expected Output: [[2, '*', 2], [2, '*', 2]]
Explanation: The seeded shuffle places mines at flat indices 1 and 4, which are the two middle-column cells.
Input: (3, 3, 3, 2)
Expected Output: [['*', 1, 0], [2, 3, 2], [1, '*', '*']]
Explanation: The seeded shuffle places mines at flat indices 8, 7, and 0.
Hints
- A flat index `p` maps to row `p // cols` and column `p % cols`.
- After marking all mines, loop over the mines and increment each valid neighbor if it is not a mine.