PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in parsing nested data structures, string processing, hierarchical representation construction, and analysis of algorithmic time and space complexity within the Coding & Algorithms domain.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Parse a nested list from a string

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a string that encodes a nested list (ArrayList-like notation), parse it into an in-memory nested list structure. ## Input format - The string contains: - `[` and `]` to denote lists - commas `,` as separators - optional whitespace - **integers** (possibly negative) - Examples: - `"[1,2,[3,-4],5]"` - `"[]"` - `"[10,[20,[30]],40]"` ## Output Return a nested list data structure where each element is either: - an integer, or - another nested list (You may describe the structure as a `NestedElement` type with two variants: `Integer(value)` or `List(children)`.) ## Constraints - Input is guaranteed to be valid. - Length of the string can be up to ~10^5 characters, so the solution should be near O(n) time. ## Follow-ups - How would you handle very deep nesting (stack depth concerns)? - What edge cases must be tested (empty list, negative numbers, multi-digit values)?

Quick Answer: This question evaluates proficiency in parsing nested data structures, string processing, hierarchical representation construction, and analysis of algorithmic time and space complexity within the Coding & Algorithms domain.

Given a string `s` that encodes a nested list using bracket notation, parse it into an in-memory nested structure. The outermost value is always a list. The string may contain: - `[` and `]` to denote lists - commas `,` as separators - optional whitespace - integers, including negative and multi-digit values Return the parsed structure as nested Python lists, where each element is either: - an integer, or - another list For example: - `"[1,2,[3,-4],5]"` -> `[1, 2, [3, -4], 5]` - `"[]"` -> `[]` - `"[10,[20,[30]],40]"` -> `[10, [20, [30]], 40]` Do not use `eval`, `json`, or other built-in parsers. Parse the string manually. Follow-up: how would you handle very deep nesting without risking recursion depth errors?

Constraints

  • The input string encodes exactly one valid outer list.
  • 2 <= len(s) <= 10^5
  • Whitespace may appear anywhere outside of numbers.
  • Integers may be negative and may contain multiple digits.

Examples

Input: "[1,2,[3,-4],5]"

Expected Output: [1, 2, [3, -4], 5]

Explanation: The string contains integers at the top level and one nested list.

Input: "[]"

Expected Output: []

Explanation: An empty list should parse to an empty Python list.

Input: "[10,[20,[30]],40]"

Expected Output: [10, [20, [30]], 40]

Explanation: This checks multiple nesting levels with multi-digit values.

Input: "[ -1, 23, [ 0, -999 ], [] ]"

Expected Output: [-1, 23, [0, -999], []]

Explanation: Whitespace should be ignored, negative values should parse correctly, and empty sublists should be preserved.

Input: "[[],[[]],-5,12345]"

Expected Output: [[], [[]], -5, 12345]

Explanation: This tests empty nested lists, a negative number, and a larger multi-digit number.

Input: " [[[1]], 2] "

Expected Output: [[[1]], 2]

Explanation: Leading and trailing whitespace should not affect parsing.

Hints

  1. Opening and closing brackets suggest using a stack to keep track of the current list being built.
  2. When you encounter a digit or a minus sign, consume the entire integer before continuing the scan.
Last updated: Apr 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)