Pattern

Parsing

Hand-write a small state machine or grammar walker instead of calling a library evaluator, and let the grammar's own structure (nesting, precedence, digit runs) drive the code's structure.

What it is

Parsing problems ask you to interpret a string according to a small grammar — an arithmetic expression, a bracket-encoded repeat scheme, a numeric literal with sign and leading-whitespace rules — without using a built-in evaluator. The technique is to name the grammar's stages explicitly (skip whitespace, read an optional sign, accumulate digits, handle an operator, descend into a nested group) and write one piece of code per stage, rather than trying to handle every case in one pass of ad hoc conditionals. Two structural tools recur constantly: a stack (or explicit save/restore) for anything that nests, and delayed commitment — holding a partially-built number or term until the next token tells you what to do with it.

When it applies

Tells in the problem

The classic mistake

The recurring bug is committing a number or term too early — adding it to a running total the moment its digits are read, before knowing whether the next character is another digit, an operator that changes its sign, or a *// that needs to fold it into the previous term instead of standing alone. The fix is uniform across every parsing problem here: keep the "current" value (a number, a term, a sub-result) in a holding variable and only commit it into the enclosing structure when you see the token that tells you how it's used — an operator, a closing bracket, or the end of the string. A second common bug is losing state across a nesting boundary — forgetting to save the sign or partial result before descending into a ( or [, so the enclosing context is wrong or lost by the time you return to it.

Questions to start with

All 4 parsing questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.