Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Nested list weight sum and Basic calculator



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Weighted Sum of Nested Integers

Weighted Sum of Nested Integers

Problem Statement

You are given a nested structure of integers, where elements can be either single integers or lists containing more integers or lists. Your task is to calculate the total sum of all integers multiplied by their depth level in the nesting.

The depth level starts at 1 for the outermost list and increases by 1 for each level of nesting inside.

Examples

Example 1:

Input: [[2, 2], 3, [2, 2]]
Output: 15
Explanation: Four 2's at depth 2 (2*2*4=16), one 3 at depth 1 (3*1=3), total is 16 + 3 = 19.

Example 2:

Input: [3, [5, [7]]]
Output: 38
Explanation: One 3 at depth 1 (3*1=3), one 5 at depth 2 (5*2=10), one 7 at depth 3 (7*3=21); sum is 3 + 10 + 21 = 34.

Constraints

  • The depth of nesting will not exceed 15.
  • The total number of integers in all nested lists combined will be at most 300.