FastPrepDepth-Weighted Sum of a Nested List
Problems/Depth-Weighted Sum of a Nested List

Depth-Weighted Sum of a Nested List

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem adds up every integer of a nested list, each one multiplied by how many lists enclose it, with the list given as text. An Adobe CS 2 report names it, with a link, as the first of two first-round coding questions and pastes the task with two examples.

Our choices: the list arrives as compact text (so reading it is part of the task), integers from -100 to 100, at most 50 levels and 5000 characters, empty lists allowed, and new examples (the pasted ones are not reused).

The judged task matches the report at about 90%.

You get a list written as text, for example [3,[5,-2],[[4]]]. Its items are integers and smaller lists, and those smaller lists can hold more lists, mixed with integers, many levels deep.

Give each integer a weight equal to how many lists enclose it: integers placed directly in the outer list get weight 1, those one list further in get weight 2, and so on.

Return the sum of every integer times its weight.

Function

depthSum(nestedList: String) → int

Examples

Example 1

nestedList = "[3,[5,-2],[[4]]]"return = 21

3 is inside one list (depth 1), 5 and -2 are inside two (depth 2), and 4 is inside three (depth 3): 3 x 1 + 5 x 2 + (-2) x 2 + 4 x 3 = 3 + 10 - 4 + 12 = 21.

Example 2

nestedList = "[[],7,[[]]]"return = 7

Empty lists hold no integers and add nothing. Only 7 counts, at depth 1: the sum is 7.

Example 3

nestedList = "[[[10]],[[-10]],1]"return = 1

10 and -10 are both at depth 3 and cancel out (30 - 30 = 0); 1 at depth 1 adds 1. The sum is 1.

Constraints

  • 2 <= nestedList.length <= 5000
  • nestedList is one list written as compact text: square brackets, commas and integers only, with no spaces (empty lists such as [] are allowed)
  • every integer is between -100 and 100, written without leading zeros or a plus sign
  • lists are nested at most 50 deep (the outer list is depth 1)

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int depthSum(String nestedList) {
    // Write your code here.
}
Checking account…