Beyond Big O

One-pass string parser

One-pass string parser

Medium

Part 1 only.

Solution guide

Reveal solution guide — spoilers

Approach

  1. Skip permitted whitespace using one forward cursor. Read the maximal atom and preserve its spelling.
  2. Skip trailing whitespace and classify the next character immediately. Return no partial value on an error.

Why it works

The cursor separates consumed characters from unread input. Each successful reader consumes exactly one value and leaves the next character for its caller.

Complexity

O(n) time. Read forward without building a separate token list; returned values account for output storage.

Edge cases to check

  • Whitespace-only input reports expected_expression at EOF.
  • Another atom after a complete root is trailing_input; stop at that first violation.
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…