Parse a Nested Integer List Literal Without a JSON Library
Last updated: November 29, 2026
Quick Overview
Given a string that is either an integer or a bracketed, comma-separated list whose elements are integers or further lists, such as [12,[-3,[4]],[]], parse it into native nested Python lists and ints without using json or eval.
Meta
November 29, 20260
0
0 solved
Given a string that is either an integer or a bracketed, comma-separated list whose elements are integers or further lists, such as [12,[-3,[4]],[]], parse it into native nested Python lists and ints without using json or eval.
Meta services pass around nested config and feature payloads, and writing a tiny parser shows whether a candidate can manage a stack or recursion over characters. Interviewers check negative and multi-digit numbers, empty lists, and the bare-integer input.
What the Interviewer Expects
- Handle the bare integer case first
- Use a stack of open lists
- Accumulate multi-digit and negative numbers
- Flush a pending number at ',' and ']'
- Support empty lists
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you report a malformed input with a position?
- How would you write it recursively and what are the depth limits?
- How would you add strings with escaped quotes?
- How would you parse lazily from a stream?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input is valid: digits,
-,[,],,, no spaces. - Output: an
intfor a bare number, otherwise a nested Python list. - Numbers may be negative and multi-digit....
High-Level Design
If the string does not start with [, it is a bare integer: return int(s).
Otherwise scan with a stack of lists and a num_start index for the number being read:
[: push a new empty list (an...