FastPrepLisp Expression Parser

Lisp Expression Parser

Amazon logoAmazon● MediumFULLTIMEONSITE INTERVIEW

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 [].

The problem statement continues
Pro

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.

FastPrep Pro
Reported in 1 Amazon interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See Amazon hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Amazon interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week