v9.1.1
Menu

Nested List Weight Sum

mediumFREENested Structures & IteratorsLinkedIn DSA

Given a nested list of integers, return the sum where each integer is multiplied by its depth. The outermost list is depth 1.

Input: nested list as JSON string.

Example

Input
[[1,1],2,[1,1]]
Output
10
Reasoning
  • The input [[1,1],2,[1,1]] is a nested list with integers at different depths.
  • We calculate the weighted sum by multiplying each integer by its depth:
    • The inner lists [1,1] are at depth 2, so their integers contribute 1⋅2+1⋅2=41 \cdot 2 + 1 \cdot 2 = 4 each.
    • The integer 2 is at depth 1, so it contributes 2⋅1=22 \cdot 1 = 2.
    • The last inner list [1,1] is also at depth 2, contributing another 1⋅2+1⋅2=41 \cdot 2 + 1 \cdot 2 = 4.
  • The total sum is then calculated as 4+2+4=104 + 2 + 4 = 10.
  • The final output is 1010.

Constraints

  • 1 <= total elements <= 50
  • Nesting depth <= 50
  • -100 <= integer value <= 100

More from LinkedIn DSA

solution.py

Test results

Sign in to run your code against the test cases.