Compute nested depth sum and grid distance
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This pair of problems evaluates algorithmic proficiency with nested data structures and grid-based graph traversal, covering concepts such as depth-weighted aggregation and shortest-path computation under obstacles.
Weighted Sum of Integers in a Nested List
Constraints
- Total number of integers across all nesting: up to ~1e5
- Nesting depth may be up to ~1e3
- Integers may be negative
- An empty list returns 0
Examples
Input: ([1, [4, [6]]],)
Expected Output: 27
Explanation: 1*1 + 4*2 + 6*3 = 1 + 8 + 18 = 27.
Input: ([],)
Expected Output: 0
Explanation: Empty list contributes nothing.
Input: ([5],)
Expected Output: 5
Explanation: Single integer at depth 1: 5*1 = 5.
Input: ([1, 2, 3],)
Expected Output: 6
Explanation: All at depth 1: 1+2+3 = 6.
Input: ([[1, 1], 2, [1, 1]],)
Expected Output: 10
Explanation: Depth-2 ones: (1+1)*2 + (1+1)*2 = 8; depth-1 two: 2*1 = 2; total 10.
Input: ([1, [-4, [6]]],)
Expected Output: 11
Explanation: 1*1 + (-4)*2 + 6*3 = 1 - 8 + 18 = 11 (negative values allowed).
Input: ([[[[10]]]],)
Expected Output: 40
Explanation: Single integer nested at depth 4: 10*4 = 40.
Hints
- Process the list recursively, passing the current depth down to children. Increment the depth by 1 each time you descend into a sub-list.
- For each element: if it is a list, recurse with depth+1 and add the result; if it is an integer, add value * depth.
- If recursion depth is a concern (up to 1e3), an explicit stack of (list, depth, index) works too and avoids Python recursion limits.
Best Empty Cell Minimizing Distance to All Buildings
Constraints
- 1 <= m, n <= 50
- grid[i][j] is one of 0 (empty), 1 (building), 2 (obstacle)
- Movement is 4-directional through empty cells only
- Return -1 if no empty cell can reach every building
Examples
Input: ([[1, 0, 2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]],)
Expected Output: 7
Explanation: An optimal empty cell reaches all three buildings with a summed shortest distance of 7.
Input: ([[1, 0, 0]],)
Expected Output: 1
Explanation: Single building; the adjacent empty cell has total distance 1 (the nearer cell is chosen).
Input: ([[1, 2, 0]],)
Expected Output: -1
Explanation: The obstacle blocks the only path, so no empty cell can reach the building.
Input: ([[1, 1], [1, 1]],)
Expected Output: -1
Explanation: There are no empty cells to start from.
Input: ([[1, 0, 1]],)
Expected Output: 2
Explanation: The middle empty cell is distance 1 from each building: 1 + 1 = 2.
Input: ([[0, 1, 2, 1, 0]],)
Expected Output: -1
Explanation: The obstacle splits the grid; no single empty cell can reach both buildings.
Hints
- Run a BFS from each building, not from each empty cell. From a building, BFS spreads only through empty (0) cells, accumulating the distance into every reachable empty cell.
- Keep two grids: total_dist[i][j] accumulates the summed distance from all buildings, and reach[i][j] counts how many buildings reached that cell.
- After all BFS runs, a cell is valid only if reach[i][j] equals the total building count (it can reach every building). Take the minimum total_dist among valid cells; if none, return -1.