FastPrepSingle-Pass S-Expression Parser
Problems/Single-Pass S-Expression Parser

Single-Pass S-Expression Parser

Baseten logoBaseten● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Baseten full-time onsite interview. The report asks for parsing in one pass without split. It starts with atoms, moves to expressions with parentheses that nest, and stops right away on a bad character. We chose the letters allowed in an atom, the grammar of proper lists, how the output is cleaned up, the limits, and the format for an error position. Our version is about 85% the same as the reported question.

Complete parseSExpression: given one simplified S-expression text, return it in the clean format below, or the position of its first error. Read text once from left to right, keeping its atoms and nested lists. Do not use split or break the whole input into tokens first.

The grammar is:

  • An atom is a nonempty run of ASCII letters, digits, or _ + - * /.
  • A list is (, then zero or more expressions, then ).
  • ASCII spaces, tabs, carriage returns and newlines may separate expressions and surround the one root expression. Parentheses also separate atoms, so whitespace next to a parenthesis is optional.

In the output, keep every atom exactly as written, drop all whitespace from the input, and put exactly one space between neighboring list elements. An empty list becomes ().

Stop at the first syntax error and return ERROR@i, where i is the zero-based index in the input where the error is found. Errors include an unsupported character, an unmatched closing parenthesis, an extra root expression, or input that ends too early. If the expression is still incomplete when the input ends, i is text.length. An empty or whitespace-only input also reports that end position.

Function

parseSExpression(text: String) → String

Examples

Example 1

text = "  (* 2 (+ 3 4))\n"return = "(* 2 (+ 3 4))"

The leading spaces and the trailing newline are dropped, and the nested lists stay as written.

Example 2

text = "(a @ b)"return = "ERROR@3"

@ is not allowed anywhere in the grammar, and it sits at index 3, so parsing stops there.

Constraints

  • 0 <= text.length <= 10000.
  • Every input character is printable ASCII, a tab, a carriage return or a newline. Printable characters outside the grammar may appear and must produce an error.
  • Parenthesis nesting in any valid prefix is at most 200.
  • The input may be malformed.

More Baseten interview questions

  • API Call Thread Pool ScheduleONSITE INTERVIEW · Seen Aug 2026
  • RNG-Based Tree Document CodecPHONE SCREEN · Seen Oct 2026
  • XOR Pseudorandom SequencePHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Evaluate an Arithmetic Expression with DivisionMotive · ONSITE INTERVIEWMotive interview questions
  • Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
  • Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
  • Flatten JSON With Array IndicesAdobe · PHONE SCREENAdobe interview questions
  • Decode StringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
  • Plus and Minus CalculatorIndeed · PHONE SCREENIndeed interview questions
  • Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions

Interview reports that mention this question

  • Baseten Experienced MLE Onsite: 1 Experience
All Baseten interview questionsSee Baseten hiring insights
Saved
class Solution {
    public String parseSExpression(String text) {
        // Write your code here.
    }
}
Checking account…