DIY: Nested List Weight Sum
Solve the interview question "Nested List Weight Sum" in this lesson.
We'll cover the following
Problem statement
For this challenge, you are given a nested list of integers. A nested list is a data structure in which each element is either an integer or a list whose elements may also be integers or other lists.
Your task is to implement a function that takes a nested list as input and returns the sum of each integer in the list multiplied by its depth. Here, an integer’s depth is the number of lists that it is nested inside of. For example, in the nested list [1,[[3,[4]],2],1]
, each integer’s value is set to its depth.
Input
The function’s input will be a list of nested integers. The following is an example of the input list:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.