String to Integer (atoi)
A medium Strings interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Strings
- Sheets
- 1
- Core for
- 14 roles
The problem
Convert a string to a 32-bit signed integer, following the rules of the standard atoi function: skip leading whitespace, handle optional sign, read digits until a non-digit is encountered.
Example 1
- Input
- "42"
- Output
- 42
Example 2
- Input
- " -42"
- Output
- -42
Example 3
- Input
- "plus 55 and more"
- Output
- 0
Constraints
- 0 <= s.length <= 200
- s consists of digits, spaces, and '+' or '-' signs
How to think about it
Updated 2026-09-29Parse characters sequentially as a finite state automaton with four phases: skip whitespace, read at most one optional sign, collect consecutive digits, and stop on any other character. Clamping to 32-bit signed limits must be caught right before an arithmetic overflow occurs rather than after an illegal integer wraps around.
Approaches, worst first
Regex extraction with large integer parse
time O(n) · space O(n)
Match the prefix format using a regular expression to extract leading spaces, an optional sign, and digit sequences. Converting the matched text into an unbounded integer or string buffer before manual clamping incurs extra allocation overhead and regex engine execution costs.
Direct pointer scan with overflow guardWrite this one
time O(n) · space O(1)
Skip leading spaces with a pointer, record sign if '+' or '-' follows, then process digits one by one. Check if current accumulator exceeds (INT_MAX - digit) / 10 before multiplying by 10; clamp immediately to INT_MIN or INT_MAX upon overflow.
Where people lose marks · 3
- Empty string or string containing only spaces: must return 0 without accessing out-of-bounds indices.
- Sign followed by non-digits like '+-12' or '+abc' must evaluate to 0 immediately.
- 32-bit integer overflow: checking for overflow after multiplying by 10 is too late if integers overflow into negative numbers or trigger undefined behavior.
The theory behind it
Strings — the ground this problem stands on. All Strings problems
What Strings is
A string is an ordered necklace of text characters, like letters printed along a ribbon of paper. Each character sits at an exact numeric slot, holding a glyph such as a letter, punctuation mark, or digit. In many programming languages, ribbons cannot be edited after creation, meaning changing a single character requires pressing an entirely new ribbon from scratch.
When to reach for it
Reach for string techniques when inputs consist of words, DNA sequences, serialized data formats, or sentences. Clues include questions testing palindromes, anagram matches, substring patterns, parenthesis balancing, or character frequency counts. Whenever an algorithm asks to transform capitalization, parse structured tokens, or compute edits between two phrases, string representations are the core subject.
How the pattern works
Think of characters as small integer codes ranging across standard character sets. Frequency tables with fixed sizes often replace heavy hash maps when tallying occurrences. For search tasks, maintain rolling state using character indices or sliding borders. When building output text through repeated appends, accumulate pieces inside a mutable list or string builder rather than concatenating strings directly, avoiding quadratic copy overhead.
More Strings problems
Target Roles
Core requirement for 14 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .