DIY: Nested Array Weight Sum

Solve the interview question "Nested List Weight Sum" yourself in this lesson.

We'll cover the following

Problem statement

For this challenge, you are given a nested array of integers. A nested array is a data structure in which each element is either an integer or an array whose elements may also be integers or other arrays.

Your task is to implement a function that takes a nested array as input and returns the sum of each integer in the array multiplied by its depth. Here, an integer’s depth is the number of arrays that it is nested inside of. For example, in the nested array [1,[[3,[4]],2],1], each integer’s value is set to its depth.

Input

The function’s input will be a array of nested integers. The following is an example of the input array:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.