FastPrepParse and Sum a Depth-Weighted Nested List
Problems/Parse and Sum a Depth-Weighted Nested List

Parse and Sum a Depth-Weighted Nested List

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Our version is about 99% the same as the reported question.

You are given a valid bracketed string nestedList representing a nested list of signed integers. Lists use square brackets, elements are comma-separated, and optional spaces may appear anywhere outside an integer token.

The outermost list has depth 1. Each nested list increases the depth of its integer elements by one. Return the sum of every integer multiplied by its depth.

Your parser must handle multi-digit values, optional minus signs, closing brackets, and an integer immediately followed by the end of the string.

Function

depthSum(nestedList: String) → int

Examples

Example 1

nestedList = "[3, 8, [2, 14], [2, [91]]]"return = 320

The weighted sum is 3 + 8 + 2*2 + 14*2 + 2*2 + 91*3 = 320.

Example 2

nestedList = "[1,[4,[6]]]"return = 27

The weighted sum is 1*1 + 4*2 + 6*3 = 27.

Example 3

nestedList = "[-12, [5], 10]"return = 8

The integers contribute -12*1 + 5*2 + 10*1 = 8.

Constraints

  • nestedList is a syntactically valid list expression.
  • Every integer token fits in a signed 32-bit integer.
  • The final weighted sum fits in a signed 32-bit integer.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEW
  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEW
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEW
  • Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEW
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEW
  • Parse Nested HTML into a TreeApple · ONSITE INTERVIEW
  • Lisp Expression ParserAmazon · ONSITE INTERVIEW
  • Lisp Expression ParserAttentive · ONSITE INTERVIEW
  • Flatten Nested Map Paths with a StackSalesforce · ONSITE INTERVIEW
See Google hiring insights
Saved
class Solution {
    public int depthSum(String nestedList) {
        // Write your code here.
    }
}
Checking account…