Posts

Showing posts from October, 2022

Product Sum - Interviewbit

Product Sum Understanding the problem We are given a "special" array, which contains integers and optionally other "special" arrays. We are asked to write a function that is going to return the sum of the elements in the "special" array. If the element is a "special" array, we need to sum up the elements in the "special" array and then multiply the sum by the depth of the "special" array. For instance, if the input array is  [3, [4, 5]] , then the result should be  3 + 2 * (4 + 5) = 21 ; if the input array is  [6, [4, [5]]] , then the result should be  6 + 2 * (4 + 3 * 5) = 44 . Approach One might notice that the "special" array has a recursive structure. Therefore, the problem can be solved recursively. We are going to have a recursive function which takes in a "special" array and the depth of the "special" array as arguments. The depth of the initial input array is going to be  1 . The recursive