Compute a Depth-Weighted Sum of a Nested List
GoogleSoftware Engineer · OnsiteLast updated
Implement and compare recursive and iterative traversals to compute the depth-weighted sum of an arbitrarily nested list of integers, handling deep nesting and edge cases.
Problem Statement
You are given an unevenly nested list of integers nested_list. Each element is either an integer or another list containing integers or further nested lists.
The depth of an integer is defined as the number of lists it is nested inside, plus one. For example, elements in the outermost list have depth 1, elements inside a list at the outermost level have depth 2, and so on.
Compute the depth-weighted sum of all integers in the list, where each integer is multiplied by its depth before summing.
Write a solution that can handle deeply nested structures without running into call stack overflow limits.
Function Signature
def depth_weighted_sum(nested_list: list) -> int:
pass
Examples
Example 1:
Input: nested_list = [[1, 1], 2, [1, 1]]
Output: 10
Explanation:
Four 1's at depth 2 (weight 2): 4 * (1 * 2) = 8
One 2 at depth 1 (weight 1): 1 * (2 * 1) = 2
Total sum = 8 + 2 = 10
Example 2:
Input: nested_list = [1, [4, [6]]]
Output: 27
Explanation:
1 at depth 1: 1 * 1 = 1
4 at depth 2: 4 * 2 = 8
6 at depth 3: 6 * 3 = 18
Total sum = 1 + 8 + 18 = 27
Example 3:
Input: nested_list = [[-1, [-2]], 3]
Output: -5
Explanation:
-1 at depth 2: -1 * 2 = -2
-2 at depth 3: -2 * 3 = -6
3 at depth 1: 3 * 1 = 3
Total sum = -2 + (-6) + 3 = -5
Constraints
- The total number of integers and nested lists is between
0and10^5. - The maximum nesting depth can be up to
10^5. - Values of integers are between
-10^4and10^4.
Edge Cases to Consider
- Empty lists at any level (e.g.,
[],[[], [[]]]), which should contribute0. - Negative integers and zeros.
- Nesting depths exceeding default recursion limits (e.g., Python's standard recursion limit of 1,000).
Hints
Hint 1
Track the depth explicitly as an integer parameter that increments by 1 each time you traverse into a sublist.
Hint 2
A standard recursive DFS will fail with a RecursionError if nesting depth reaches thousands; an explicit stack or queue avoids call stack limits.
Hint 3
With an iterative approach, push pairs of `(element, depth)` onto a stack or queue, adding child elements with `depth + 1` whenever an element is a list.
Follow-up questions
- How would you compute the reverse depth-weighted sum, where the deepest elements have weight 1 and the outermost elements have the maximum weight?
- How do the memory footprints of BFS (level-order traversal) and DFS (stack-based) compare when the nested list is extremely deep versus extremely wide?
- How would you handle this if the input were given as a streaming iterator or generator rather than an in-memory list?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.