PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Compute weighted sum of nested lists states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Compute weighted sum of nested lists

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a nested collection that can contain integers or other nested collections, compute the depth-weighted sum where each integer is multiplied by its 1-based depth. Implement both DFS and BFS solutions, analyze time and space complexity, and discuss how you would adapt the approach if the weighting is inverted (deepest level has weight 1).

Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Compute weighted sum of nested lists states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a nested collection that can contain integers or other nested collections, compute the depth-weighted sum where each integer is multiplied by its 1-based depth. The top level has depth 1, a list nested one level deeper has depth 2, and so on. For example, given [[1,1],2,[1,1]]: the integer 2 sits at depth 1 (weight 1) and the four 1s sit at depth 2 (weight 2), so the result is 2*1 + 1*2 + 1*2 + 1*2 + 1*2 = 10. Given [1,[4,[6]]]: 1*1 + 4*2 + 6*3 = 27. Implement the function. (Interview follow-ups: provide both a DFS and a BFS solution, analyze time/space complexity, and describe how you would adapt the approach if the weighting is inverted so the deepest level has weight 1 — this requires a first pass to find max depth, then weight each integer by (maxDepth - depth + 1).) The input is modeled as a nested list of integers (a value is either an integer or a list of such values).

Constraints

  • Each element of the collection is either an integer or a nested list of such elements.
  • Nesting depth >= 1; the top-level list is depth 1.
  • Integers may be negative, zero, or positive.
  • The collection may be empty, in which case the sum is 0.

Examples

Input: ([[1,1],2,[1,1]],)

Expected Output: 10

Explanation: 2 at depth 1 (=2) plus four 1s at depth 2 (=8) gives 10.

Input: ([1,[4,[6]]],)

Expected Output: 27

Explanation: 1*1 + 4*2 + 6*3 = 1 + 8 + 18 = 27.

Input: ([],)

Expected Output: 0

Explanation: Empty collection contributes 0.

Input: ([5],)

Expected Output: 5

Explanation: Single integer at depth 1: 5*1 = 5.

Input: ([[[[3]]]],)

Expected Output: 12

Explanation: 3 is nested four levels deep: 3*4 = 12.

Input: ([-1,[-2,[-3]]],)

Expected Output: -14

Explanation: -1*1 + -2*2 + -3*3 = -1 - 4 - 9 = -14, confirming negatives work.

Input: ([0,[0,[0]]],)

Expected Output: 0

Explanation: All zeros sum to 0 regardless of depth weighting.

Input: ([[1,[2,[3,[4]]]]],)

Expected Output: 40

Explanation: 1*2 + 2*3 + 3*4 + 4*5 = 2 + 6 + 12 + 20 = 40 (asymmetric deep nesting).

Hints

  1. DFS: recurse into each sublist carrying the current depth; when you hit an integer, add value * depth.
  2. BFS: process the structure level by level with a queue; everything dequeued at level L contributes value * L. Increment the depth after each level is fully processed.
  3. Inverted weighting (deepest level = weight 1): first compute the maximum depth, then weight each integer by (maxDepth - depth + 1). This can also be done in a single BFS pass by accumulating an unweighted running total at each level and re-adding it every subsequent level — a clever O(n) one-pass trick.
  4. Watch the base case: an empty list contributes 0, and a single integer at the top level is weighted by depth 1.
Last updated: Jun 26, 2026

Loading coding console...

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.

Related Coding 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)