Skip to content
core-pattern

State Tracking & Invariants

Track the smallest evolving state needed to preserve a scan or simulation invariant.

Problems

Problems in State Tracking & Invariants

Practice problems that share this primary solution pattern and compare the clues that reveal it.

A stylish workspace featuring a laptop, plant, and smartphone on a desk.
intermediate
10 min read

Count and Say

The Count and Say solution is a repeated state transition: start with "1", scan the current string into maximal consecutive runs, and emit each run as…

View solution
An artistic arrangement of golden gears on a dark backdrop, symbolizing mechanics and cooperation.
beginner
9 min read

Longest Common Prefix

The trap is to compare whole strings before identifying the stopping condition. The answer ends at the first column where agreement breaks—or when the…

View solution
Black computer cables splayed on a vibrant yellow surface, highlighting technology connection themes.
advanced
11 min read

Next Permutation

The reliable way to solve Next Permutation is to stop thinking in four memorized steps. Read the suffix, identify where it is already maximal, then make…

View solution
A basket with dried yellow flowers and herbs on a table, ideal for tea or decoration.
beginner
10 min read

Palindrome Number

A full reverse is easy to write. The interview-level move is to stop reversing when the two halves meet.

View solution
Asian students in uniform learning in a computer lab, focused on their tasks.
intermediate
11 min read

Reverse Integer

Reversing digits is easy. Reversing them without ever creating an unsafe intermediate value is the interview problem.

View solution
Close-up of a blue screen error shown on a data center control terminal.
beginner
10 min read

Roman to Integer

Adding every Roman symbol works for LVIII, but it turns IV into 6. The reliable Roman to Integer solution is a left-to-right scan with one local question:…

View solution
Detailed shot of microchips on a circuit board, showcasing electronic technology and precision engineering.
intermediate
10 min read

String to Integer (atoi)

A reliable atoi parser is a small state machine: skip leading spaces, read one optional sign, consume one numeric prefix, and guard every accumulator…

View solution
A neat workspace featuring a laptop displaying Google search, a smartphone, and a notebook on a wooden desk.
intermediate
11 min read

Zigzag Conversion

A visual zigzag is easy to draw and surprisingly easy to implement incorrectly. The reliable solution is smaller: track the current row, track the movement…

View solution
A person working on a laptop with a red notebook and glasses on a white table.
intermediate
10 min read

Add Two Numbers

The lists already expose digits in the order addition needs. Scan both lists together, track one carry, and keep going until there is no digit or carry…

View solution
Lush green pine tree with a vibrant blue sky background, perfect for nature-themed projects.
beginner
11 min read

Add Binary

You receive two binary strings, a and b, and must return their sum as another binary string. The inputs contain only '0' and '1', have lengths from 1 to…

View solution
Expansive desert landscape with sparse vegetation under a clear blue sky, capturing the arid beauty.
beginner
9 min read

Length of Last Word

The final character may be a space, so the right edge of the string is not necessarily part of the answer. The clean Length of Last Word solution is a…

View solution
Detailed view of tree trunk with visible growth rings, showcasing natural wood texture.
intermediate
10 min read

Multiply Strings

Treat the product as a fixed array of decimal positions. Every digit pair has a predictable destination; carry normalization keeps those positions valid.

View solution
Business professionals reviewing analytics on a tablet during a meeting.
beginner
9 min read

Plus One

Adding one looks trivial until the last digit is 9. Then the problem becomes a compact state-tracking exercise: start where arithmetic begins, propagate a…

View solution
A broken laptop screen displayed with colorful glitch being held by a person.
advanced
12 min read

Text Justification

A line can contain the correct words and still be wrong: one space in the wrong gap, one missing trailing space, or full justification applied to the final…

View solution
Two ancient columns stand majestically against a serene hillside backdrop.
advanced
12 min read

Valid Number

A decimal point and an exponent are easy to track. The hard part is proving that every numeric component actually contains the digits the grammar requires.

View solution