PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Compute nested depth sum and grid distance

Last updated: Mar 29, 2026

Quick Overview

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.

  • medium
  • Meta
  • Coding & Algorithms
  • Machine Learning Engineer

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.

Related Interview Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
Meta logo
Meta
Dec 15, 2025, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
8
0

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

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Machine Learning Engineer•Meta Machine Learning Engineer•Meta Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.