PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of tree-like nested data structures, traversal strategies (recursive DFS and iterative approaches), handling of edge cases such as empty lists and negative integers, and algorithmic reasoning about time and space complexity within the Coding & Algorithms domain.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Compute nested depth-weighted sum

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

You are given a nested structure containing integers and lists (e.g., [1, [4, [6]]]). Define depth of the top-level as 1. Compute the total sum of all integers multiplied by their depth. Handle empty lists, large depth, and negative integers. Provide both recursive (DFS) and iterative (BFS/stack) approaches, analyze time and space complexity, and discuss how you would design the interface for accessing nested elements.

Quick Answer: This question evaluates understanding of tree-like nested data structures, traversal strategies (recursive DFS and iterative approaches), handling of edge cases such as empty lists and negative integers, and algorithmic reasoning about time and space complexity within the Coding & Algorithms domain.

You are given a nested structure containing integers and lists, for example `[1, [4, [6]]]`. The depth of the top-level elements is 1, and each level of nesting increases the depth by 1. Compute the total sum of every integer multiplied by its depth. For `[1, [4, [6]]]` the answer is `1*1 + 4*2 + 6*3 = 27`. Your function must handle empty lists (which contribute nothing), arbitrarily large depth, and negative integers. Both a recursive DFS and an iterative stack-based traversal compute the same result; the reference solution shows the recursive form, and you should be able to reason about the iterative form as well. In this console the nested structure is provided as a JSON-style string (e.g. `"[1, [4, [6]]]"`) so it can be passed across languages. Parse it into a nested list/array and return the depth-weighted sum as an integer.

Constraints

  • The nested structure contains only integers and (possibly empty) lists.
  • Top-level elements are at depth 1; each level of nesting adds 1.
  • Empty lists contribute 0 to the sum.
  • Integers may be negative.
  • Nesting depth and element count can be large; avoid stack overflow concerns by reasoning about an iterative approach for very deep inputs.

Examples

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

Expected Output: 27

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

Input: [[1, 1], 2, [1, 1]]

Expected Output: 10

Explanation: Each inner 1 is at depth 2 and the standalone 2 is at depth 1: 4*1*2 + 2*1 = 8 + 2 = 10.

Input: [1, [4, [6]], [[[5]]]]

Expected Output: 47

Explanation: 1*1 + 4*2 + 6*3 + 5*4 = 1 + 8 + 18 + 20 = 47.

Input: []

Expected Output: 0

Explanation: An empty top-level list has no integers, so the sum is 0.

Input: [[], [], []]

Expected Output: 0

Explanation: Only empty lists at any depth — no integers contribute.

Input: [5]

Expected Output: 5

Explanation: A single integer at depth 1: 5*1 = 5.

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

Expected Output: -14

Explanation: Negative integers: -1*1 + -2*2 + -3*3 = -1 - 4 - 9 = -14.

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

Expected Output: 30

Explanation: Depths 1,2,3,4: 1*1 + 2*2 + 3*3 + 4*4 = 1 + 4 + 9 + 16 = 30.

Hints

  1. Carry the current depth as a parameter in your DFS; integers contribute value * depth and lists recurse with depth + 1.
  2. An empty list naturally contributes nothing because the loop over its (zero) children adds nothing.
  3. For the iterative version, push (item, depth) pairs onto a stack; pop, and if the item is a list push each child with depth + 1.
  4. Negative integers need no special handling — multiplication by depth preserves the sign.
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)