Skip to main content

339. Nested List Weight Sum

mediumAsked at LinkedIn

Given a nested list of integers, return the sum of all integers weighted by their depth. LinkedIn asks this on the recursion round — they want clean DFS or BFS code that uses the provided NestedInteger interface without unpacking it eagerly.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in LinkedIn loops.

  • Glassdoor (2026-Q1)LinkedIn SWE onsite reports list Nested List Weight Sum (and II) as a LinkedIn signature.
  • Blind (2025-12)LinkedIn writeups specifically cite the NestedInteger interface and the depth-tracking recursion as the explicit signal.

Problem

You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. The depth of an integer is the number of lists that it is inside of. For example, the nested list [1,[2,2],[[3],2],1] has each integer's value set to its depth. Return the sum of each integer in nestedList multiplied by its depth.

Constraints

  • 1 <= nestedList.length <= 50
  • The values of the integers in the nested list is in the range [-100, 100].
  • The maximum depth of any integer is less than or equal to 50.

Examples

Example 1

Input
nestedList = [[1,1],2,[1,1]]
Output
10

Explanation: Four 1's at depth 2, one 2 at depth 1. 4 * 1 * 2 + 1 * 2 * 1 = 10.

Example 2

Input
nestedList = [1,[4,[6]]]
Output
27

Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1*1 + 4*2 + 6*3 = 27.

Approaches

1. Recursive DFS with depth parameter (preferred)

Walk the list; for each item, if it's an integer add value * depth; if it's a list, recurse with depth + 1.

Time
O(N) where N is total items
Space
O(D) recursion where D is max depth
function depthSum(nestedList) {
  function helper(list, depth) {
    let sum = 0;
    for (const item of list) {
      if (item.isInteger()) sum += item.getInteger() * depth;
      else sum += helper(item.getList(), depth + 1);
    }
    return sum;
  }
  return helper(nestedList, 1);
}

Tradeoff: Easiest to write; depth tracking is automatic via the recursion parameter. The interview-default version.

2. BFS layer-by-layer

Use a queue. At each layer, process all items at the current depth, increment depth between layers.

Time
O(N)
Space
O(N) for the queue
function depthSumBFS(nestedList) {
  let queue = nestedList.slice();
  let depth = 1, total = 0;
  while (queue.length) {
    const next = [];
    for (const item of queue) {
      if (item.isInteger()) total += item.getInteger() * depth;
      else next.push(...item.getList());
    }
    queue = next;
    depth++;
  }
  return total;
}

Tradeoff: Iterative — useful if the interviewer asks for non-recursive. Slightly more memory but avoids deep recursion stack issues. Both are O(N) time.

LinkedIn-specific tips

LinkedIn interviewers want you to use the NestedInteger interface methods (isInteger, getInteger, getList) rather than try to unpack the structure manually. Articulate: 'I'll lean on the provided API and pass depth through recursion as a parameter.' Be ready for the II follow-up where weight is inverted (deepest = weight 1) — different algorithm, requires two passes or a postorder accumulation.

Common mistakes

  • Starting depth at 0 instead of 1 — gives 0 for top-level integers.
  • Trying to flatten nestedList first then sum — defeats the purpose; loses the depth info.
  • In the BFS version, using a single queue and an inner counter — gets tangled when nested lists span multiple depths.

Follow-up questions

An interviewer at LinkedIn may pivot to one of these next:

  • Nested List Weight Sum II (LC 364) — invert the weighting so the leaves are weight 1.
  • Flatten Nested List Iterator (LC 341) — same NestedInteger interface, iterator pattern.
  • What if the structure could have cycles? (Add a visited set, but the LC version guarantees a tree.)

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why pass depth as a parameter instead of tracking it externally?

Because recursion's natural call stack handles depth automatically. An external counter would have to be incremented and decremented around recursive calls, which is bug-prone.

What's the difference between this and LC 364?

Here, depth multiplies as you go deeper (root weight 1, deepest weight = max depth). In 364, weight is inverted — the deepest layer is weight 1 and root is the heaviest. Requires either a two-pass (find max depth, then sum) or a postorder accumulation.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Nested List Weight Sum and other LinkedIn interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →