Compute nested depth sum and grid distance
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
## Problem A: Weighted sum of integers in a nested list
You are given a nested list structure that may contain integers or other nested lists. Define the **depth** of top-level elements as 1, their children as depth 2, etc.
**Task:** Compute the sum of each integer multiplied by its depth.
**Input:**
- A nested list, e.g. `[1, [4, [6]]]`
**Output:**
- An integer weighted sum
**Example:**
- Input: `[1, [4, [6]]]`
- `1` at depth 1 → `1*1`
- `4` at depth 2 → `4*2`
- `6` at depth 3 → `6*3`
- Total = `1 + 8 + 18 = 27`
**Constraints (typical):**
- Total number of integers across all nesting: up to ~1e5
- Nesting depth may be up to ~1e3
---
## Problem B: Best empty cell minimizing distance to all buildings
You are given an `m x n` grid with values:
- `0` = empty land (you may start here)
- `1` = building
- `2` = obstacle (cannot pass)
Movement is 4-directional (up/down/left/right). Distance is the number of steps along empty cells (you cannot pass through obstacles or buildings).
**Task:** Choose an empty cell `0` such that the **sum of shortest path distances** from that cell to **every** building is minimized. Return that minimum sum.
If there is no empty cell from which all buildings are reachable, return `-1`.
**Input:**
- `grid`: `m x n` integer matrix
**Output:**
- Minimum possible sum distance, or `-1`
**Example:**
- `grid = [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]` → output `7` (one optimal empty cell has total distance 7)
**Constraints (typical):**
- `1 <= m,n <= 50`
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.