Lisp Expression Parser
Problem statement
Parse expression as one Lisp-style expression and return the canonical JSON serialization of its syntax tree as a string.
- An atom is a nonempty maximal sequence of ASCII letters, digits, underscore, plus, minus, asterisk or slash. Preserve its exact characters and case, including leading zeros and signs.
- A list consists of an opening parenthesis, zero or more expressions, and a closing parenthesis. Lists may nest.
- ASCII space, tab, line feed and carriage return separate tokens and may occur before or after the root. Parentheses are standalone tokens even when adjacent to atoms or other parentheses.
- The input is valid and contains exactly one root expression. Adjacent atoms must be separated by whitespace; parentheses already provide token boundaries.
Serialize every atom as a JSON string and every list as a JSON array of its children, preserving their order and nesting. Use commas between children and no whitespace outside quoted atom strings. The allowed atom characters never contain a quote or backslash. An empty list becomes [].
Examples
Example 1
expression = " (add 1 (mul 2 3)) "return = "[\"add\",\"1\",[\"mul\",\"2\",\"3\"]]"The outer list has three children. Its last child is another list. Operator names and numbers are all strings; no multiplication or addition is performed.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 1 more worked example, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.