Quick Overview

Parse a valid nested-list string and return the depth-weighted sum of its non-negative integers. Track bracket depth and multi-digit number boundaries correctly, including empty lists, spaces, and a final number.

Parse a Nested-List String and Compute Its Weighted Sum

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Parse a Nested-List String and Compute Its Weighted Sum ## Problem You are given a string representation of a nested list containing non-negative integers, square brackets, commas, and optional spaces. Integers in the outermost list have depth `1`; each nested list increases the depth by `1`. Parse the string and return the sum of every integer multiplied by its depth. The input is syntactically valid, and integers may contain multiple digits. ### Function Contract Implement `parseDepthWeightedSum(text)`. - Input: one valid nested-list string. - Output: the integer depth-weighted sum. ### Examples ```text Input: "[3, 8, [2, 14], [2, [91]]]" Output: 320 ``` ```text Input: "[10, [20, []], 3]" Output: 53 Calculation: 10*1 + 20*2 + 3*1 = 53 ``` ### Parsing Requirements - Opening and closing brackets change the current depth. - A multi-digit number must be accumulated before it is added. - A number is complete when a comma or closing bracket is reached; the implementation must also handle a number ending at the end of the string. - Empty lists contribute nothing. ```hint Separate number accumulation from number completion Track whether digits are currently being read so that delimiters do not create spurious zero values. ``` ```hint Update depth at the correct moment Determine whether an opening bracket changes the multiplier before or after the first number inside that list is read. ```

Quick Answer: Parse a valid nested-list string and return the depth-weighted sum of its non-negative integers. Track bracket depth and multi-digit number boundaries correctly, including empty lists, spaces, and a final number.

Parse a valid nested-list string containing non-negative integers, brackets, commas, and optional spaces. Integers in the outer list have depth one; return the sum of each integer times its depth.

Constraints

  • 2 <= text.length <= 10,000, and text is a syntactically valid nested-list representation.
  • The text contains only non-negative integers, square brackets, commas, and optional spaces.
  • Every integer is at most 10^12, and the final weighted sum fits in a signed 64-bit integer.
  • The outermost list gives its direct integer elements depth 1.

Examples

Input: ('[]',)

Expected Output: 0

Explanation: An empty outer list contributes nothing.

Input: ('[5]',)

Expected Output: 5

Explanation: One outer value has depth one.

Hints

  1. Track whether digits are currently being accumulated so delimiters never create a spurious zero.
  2. Use the current bracket depth at the moment a number ends, before closing that list level.

Loading coding console...