← Questions

Nested list weight sum

0
MetaCodingLast asked Feb 2026

A coding interview question asked in Meta's AI-enabled coding round, last asked Feb 2026. It tests recursion and dfs.

A nested list holds integers and other nested lists, to any depth. Return the sum of every integer multiplied by its depth. Integers directly in the outer list have depth 1, those inside one inner list have depth 2, and so on.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • depth_sum(nested: list) -> int — each element is an int or a list of the same kind. An empty list returns 0.

Example

[1, [4, [6]]]          ->  27    # 1*1 + 4*2 + 6*3
[[1, 1], 2, [1, 1]]    ->  10    # four 1s at depth 2, one 2 at depth 1
[]                     ->  0
[[[]]]                 ->  0

Constraints

  • Up to 10^5 integers in total; nesting up to about 10^3 levels.
  • Integers may be negative.

Clarifying questions

  • Is the input plain lists, or a NestedInteger interface with is_integer(), get_integer() and get_list()?
  • Can lists be empty?
  • Can the nesting be deep enough to overflow the recursion stack?

What a strong answer covers

  • DFS passing the current depth down: an integer adds value * depth, a list recurses with depth + 1.
  • BFS by level as an alternative, multiplying the sum of each level by its depth.
  • An explicit stack of (list, depth) when nesting is too deep for recursion.
  • O(N) time over all integers and lists, O(D) extra space for depth D.
  • Tests: empty list, empty inner lists, negative values, a single deep chain.

Follow-ups

  • Inverse weights: the deepest integers get weight 1 and the outer ones the largest weight. Can you do it in one pass?
  • The input arrives as a string such as "[1,[4,[6]]]". Parse it and compute the sum.
  • Flatten the nested list into an iterator that returns integers one at a time.

Where it's asked

  • Meta · AI-enabled coding · Last asked Feb 202660 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

recursion · dfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding