PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of nested list traversal, depth computation, and weighted aggregation by the inverse of depth, focusing on concepts like maximum depth calculation and applying weights across hierarchical integer elements.

  • medium
  • LinkedIn
  • Coding & Algorithms
  • Software Engineer

Compute inverse-depth weighted sum of nested lists

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Inverse-Depth Weighted Sum of a Nested List You are given a **nested list** of integers. Each element is either: - an integer, or - another nested list. Define the **depth** of an integer as the number of lists it is inside (top-level integers have depth 1). Compute the **inverse-depth weighted sum**: - Let `D` be the **maximum depth** in the structure. - An integer at depth `d` has weight `D - d + 1`. - Return the sum of `value * weight` over all integers. ### Input - A nested list structure (you may assume an interface like: - `isInteger()`, `getInteger()`, `getList()`), or an equivalent representation. ### Output - An integer: the inverse-depth weighted sum. ### Example Input: `[1, [4, [6]]]` - Depths: `1` at depth 1, `4` at depth 2, `6` at depth 3. Max depth `D=3`. - Weights: depth1 -> 3, depth2 -> 2, depth3 -> 1 - Sum = `1*3 + 4*2 + 6*1 = 17` ### Constraints - Total number of integers across all lists can be up to e.g. `10^4`. - Integers fit in 32-bit signed range.

Quick Answer: This question evaluates understanding of nested list traversal, depth computation, and weighted aggregation by the inverse of depth, focusing on concepts like maximum depth calculation and applying weights across hierarchical integer elements.

You are given a **nested list** of integers. Each element is either an integer or another nested list. Define the **depth** of an integer as the number of lists it is inside (top-level integers have depth 1). Compute the **inverse-depth weighted sum**: - Let `D` be the **maximum depth** present in the structure. - An integer at depth `d` has weight `D - d + 1` (so the **deepest** integers get weight 1, and shallower integers get larger weights). - Return the sum of `value * weight` over all integers. The input is provided as a nested list (a list whose elements are integers or further nested lists). ### Example Input: `[1, [4, [6]]]` - Depths: `1` at depth 1, `4` at depth 2, `6` at depth 3, so max depth `D = 3`. - Weights: depth 1 -> 3, depth 2 -> 2, depth 3 -> 1. - Sum = `1*3 + 4*2 + 6*1 = 17`. Return `17`.

Constraints

  • Total number of integers across all lists can be up to ~10^4.
  • Integers fit in 32-bit signed range.
  • An empty top-level list returns 0.
  • Deeper integers receive smaller weights; the deepest integers always have weight 1.

Examples

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

Expected Output: 17

Explanation: D=3. 1 at depth1 (w3), 4 at depth2 (w2), 6 at depth3 (w1): 1*3 + 4*2 + 6*1 = 17.

Input: ([[1, 1], 2, [1, 1]],)

Expected Output: 8

Explanation: D=2. 2 at depth1 (w2); four 1's at depth2 (w1): 2*2 + 4*1 = 8.

Input: ([1, [4, [6]], [7]],)

Expected Output: 31

Explanation: D=3. 1 at depth1 (w3), 4 at depth2 (w2), 7 at depth2 (w2), 6 at depth3 (w1): 3 + 8 + 14 + 6 = 31.

Input: ([],)

Expected Output: 0

Explanation: Empty structure: no integers, sum is 0.

Input: ([5],)

Expected Output: 5

Explanation: D=1, single integer at depth1 (w1): 5*1 = 5.

Input: ([[[[10]]]],)

Expected Output: 10

Explanation: Only one integer at the deepest level (weight 1 regardless of D): 10*1 = 10.

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

Expected Output: -14

Explanation: D=3. -3*3 + -2*2 + -1*1 = -9 - 4 - 1 = -14; negatives are handled normally.

Input: ([0, [0, [0]]],)

Expected Output: 0

Explanation: All zeros contribute nothing regardless of weight: sum is 0.

Hints

  1. Two passes are simplest: first compute the maximum depth D, then sum value * (D - depth + 1).
  2. Alternatively, run a BFS level by level, keep a running 'unweighted' accumulator that carries each level's prefix sum forward; the total accumulates each integer once per remaining level, which equals weighting by (D - depth + 1).
  3. Top-level integers are at depth 1, not depth 0 — be careful with the off-by-one in the weight formula.
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
  • AI Coding 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

  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Design O(1) Randomized Multiset - LinkedIn (easy)
  • Process Mutable Matrix Sum Queries - LinkedIn (medium)
  • Design a Randomized Multiset - LinkedIn (medium)
  • Can You Place N Objects? - LinkedIn (medium)