Nested List Weight Sum (for E4)
Updated
Problem statement
You are given a valid JSON string nestedList encoding a nested list of integers. Each list element is an integer or another list. Return the sum of every integer multiplied by the number of enclosing lists. Integers directly inside the outer list have depth 1.
For this exercise, use the JSON text as the portable input representation. Spaces may occur between tokens. Empty inner lists contribute zero. The outer list has between 1 and 50 elements, integer values are from -100 through 100, and integer depth is at most 50.
Function
depthSum(nestedList: String) → intExamples
Example 1
nestedList = "[[1,1],2,[1,1]]"return = 10Four 1s are at depth 2, and 2 is at depth 1. The weighted sum is 1 × 2 + 1 × 2 + 2 × 1 + 1 × 2 + 1 × 2 = 10.
Example 2
nestedList = "[1,[4,[6]]]"return = 27The value 1 is at depth 1, 4 is at depth 2, and 6 is at depth 3. The weighted sum is 1 × 1 + 4 × 2 + 6 × 3 = 27.
Example 3
nestedList = "[0]"return = 0The value 0 is at depth 1, so the weighted sum is 0 × 1 = 0.
Constraints
- The string is valid nested-list JSON containing only integer values, lists, commas, and optional spaces.
- The decoded outer list contains 1 through 50 elements.
- Every integer is in
[-100,100], and maximum integer depth is 50. - For this exercise, assume the JSON text has at most 100000 characters.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Balanced Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Height-Balanced Binary TreeExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Root-to-Leaf Sum in a BSTZillow · PHONE SCREENZillow interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Nested JSON Schema ValidatorPinterest · ONSITE INTERVIEWPinterest interview questions
- Validate Binary Search TreeLyft · PHONE SCREENLyft interview questions
- Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEWAdobe interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences