cswork / libraryOpen practice workspace

Coding questions · Meta

Compute weighted sum of nested lists

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Compute weighted sum of nested lists states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a nested collection that can contain integers or other nested collections, compute the depth-weighted sum where each integer is multiplied by its 1-based depth. Implement both DFS and BFS solutions, analyze time and space complexity, and discuss how you would adapt the approach if the weighting is inverted (deepest level has weight 1).

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Depth-Weighted Sum of a Nested List

Given a nested collection that can contain integers or other nested collections, compute the depth-weighted sum where each integer is multiplied by its 1-based depth. The top level has depth 1, a list nested one level deeper has depth 2, and so on.

For example, given [[1,1],2,[1,1]]: the integer 2 sits at depth 1 (weight 1) and the four 1s sit at depth 2 (weight 2), so the result is 21 + 12 + 12 + 12 + 12 = 10. Given [1,[4,[6]]]: 11 + 42 + 63 = 27.

Implement the function. (Interview follow-ups: provide both a DFS and a BFS solution, analyze time/space complexity, and describe how you would adapt the approach if the weighting is inverted so the deepest level has weight 1 — this requires a first pass to find max depth, then weight each integer by (maxDepth - depth + 1).)

The input is modeled as a nested list of integers (a value is either an integer or a list of such values).

Constraints

  • Each element of the collection is either an integer or a nested list of such elements.
  • Nesting depth >= 1; the top-level list is depth 1.
  • Integers may be negative, zero, or positive.
  • The collection may be empty, in which case the sum is 0.

Examples

Example 1

Input: ([[1,1],2,[1,1]],)
Output: 10

Explanation: 2 at depth 1 (=2) plus four 1s at depth 2 (=8) gives 10.

Example 2

Input: ([1,[4,[6]]],)
Output: 27

Explanation: 11 + 42 + 6*3 = 1 + 8 + 18 = 27.

Hints

  1. DFS: recurse into each sublist carrying the current depth; when you hit an integer, add value * depth.
  2. BFS: process the structure level by level with a queue; everything dequeued at level L contributes value * L. Increment the depth after each level is fully processed.
  3. Inverted weighting (deepest level = weight 1): first compute the maximum depth, then weight each integer by (maxDepth - depth + 1). This can also be done in a single BFS pass by accumulating an unweighted running total at each level and re-adding it every subsequent level — a clever O(n) one-pass trick.
  4. Watch the base case: an empty list contributes 0, and a single integer at the top level is weighted by depth 1.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

Given a nested collection that can contain integers or other nested collections, compute the depth-weighted sum where each integer is multiplied by its 1-based depth. Implement both DFS and BFS solutions, analyze time and space complexity, and discuss how you would adapt the approach if the weighting is inverted (deepest level has weight 1).

Recommended Approach

Model each reachable configuration as a graph state and choose the traversal by edge cost: BFS for unweighted shortest paths, Dijkstra for non-negative weighted paths, or topological DP for DAGs. Track visited states at the correct granularity so cycles do not cause repeated work.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

BFS is O(V + E) time and O(V) space for a standard graph. Expanded-state problems multiply those bounds by the number of state dimensions.

Edge Cases and Tests

Disconnected graph, source equals target, cycles, duplicate edges, unreachable target, and whether the answer counts nodes, edges, moves, or transfers.