Skip to main content
All Questions

Compute the inverse nested list weight sum in a single pass.

Unlock detailed company stats for this questionUpgrade

Given a nested list of integers where each element is an integer or another nested list, the depth of an integer is how many lists it is nested inside, and the maximum depth is that of the deepest integer. Each integer's weight is MaximumDepth - CurrentDepth + 1. Return the sum of every integer multiplied by its weight. The solution should be highly optimized, ideally one pass without first traversing the list to find the maximum depth.

Example: {1, {2, 2}, {2, {3, 2}}} has maximum depth 3. 1 at depth 1 has weight 3 (value 3); the two 2s at depth 2 have weight 2 (value 8); the other 2 at depth 2 has weight 2 (value 4); {3, 2} at depth 3 has weight 1 (value 5). Total output: 20.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.