8. String to Integer (atoi)
mediumAsked at AppleString to Integer (atoi) is Apple's adversarial parsing question. Six precise rules — skip whitespace, read optional sign, read digits, clamp to 32-bit — and you must handle every weird input the interviewer throws at you. Apple grades on edge-case coverage, not on the parse loop itself.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Apple loops.
- Glassdoor (2026-Q1)— Apple SWE phone-screen reports consistently list atoi as the canonical adversarial-input parsing medium.
- Blind (2025-12)— Apple ICT3/ICT4 reports cite atoi as the recurring 30-minute parsing medium with rich edge cases.
Problem
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). The algorithm: 1) read in and ignore any leading whitespace, 2) check if the next character is '-' or '+'; read this character if it is — this determines if the final result is negative or positive, 3) read in next the characters until the next non-digit character or the end of the input is reached; the rest of the string is ignored, 4) convert these digits into an integer (i.e., '123' -> 123). If no digits were read, then the integer is 0. Change the sign as necessary. 5) If the integer is out of the 32-bit signed integer range [-2^31, 2^31 - 1], then clamp the integer so that it remains in the range. Return the integer as the final result.
Constraints
0 <= s.length <= 200s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
Examples
Example 1
s = "42"42Example 2
s = " -42"-42Example 3
s = "4193 with words"4193Example 4
s = "words and 987"0Explanation: The first non-whitespace character is 'w' which is not a digit or a +/- sign. Therefore no valid conversion could be performed.
Approaches
1. Stepwise parse with overflow clamp (optimal)
Skip whitespace, parse sign, parse digits one at a time. Before each digit append, clamp to INT_MAX/INT_MIN if overflow would occur.
- Time
- O(n)
- Space
- O(1)
function myAtoi(s) {
const INT_MAX = 2147483647;
const INT_MIN = -2147483648;
let i = 0;
while (i < s.length && s[i] === ' ') i++;
if (i === s.length) return 0;
let sign = 1;
if (s[i] === '+' || s[i] === '-') {
sign = s[i] === '-' ? -1 : 1;
i++;
}
let num = 0;
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
const d = s.charCodeAt(i) - 48;
if (num > Math.floor(INT_MAX / 10) || (num === Math.floor(INT_MAX / 10) && d > 7)) {
return sign === 1 ? INT_MAX : INT_MIN;
}
num = num * 10 + d;
i++;
}
return sign * num;
}Tradeoff: Single linear pass with explicit phase separation: whitespace, sign, digits. The clamp on overflow is the trickiest piece — for sign=1 we clamp to INT_MAX; for sign=-1 we clamp to INT_MIN. Note INT_MIN = -2147483648 which makes the digit threshold 8, not 7, on negative — but most implementations clamp at the same threshold (7) and round the negative side by clamping to INT_MIN, which is also correct since '-2147483648' is the only string that would have that single-digit advantage.
2. Regex with post-clamp
Use a regex to extract the number prefix, parse it as a number, then clamp.
- Time
- O(n)
- Space
- O(1)
function myAtoi(s) {
const m = s.trimStart().match(/^[+-]?\d+/);
if (!m) return 0;
const n = parseInt(m[0], 10);
if (n > 2147483647) return 2147483647;
if (n < -2147483648) return -2147483648;
return n;
}Tradeoff: Works in JS because parseInt handles arbitrary length. Apple will reject this as 'using language features that hide the work' — the point of the question is to see you handle parsing manually. Mention the regex solution exists, then write the stepwise one.
Apple-specific tips
Apple loves this question because the spec has so many traps. Before writing code, list the phases out loud: 'one — skip leading spaces. Two — read optional sign. Three — read digits until non-digit. Four — clamp to 32-bit range.' Then implement each phase as its own block. When the interviewer throws 'what about leading zeros?' or '+0' or just whitespace, you can answer phase-by-phase.
Common mistakes
- Handling whitespace anywhere (not just leading) — spec says only leading.
- Treating multiple signs as valid ('++1' should return 0).
- Forgetting that '0123' returns 123 (leading zeros are fine inside the digit phase).
- Doing the overflow check after the multiply instead of before.
Follow-up questions
An interviewer at Apple may pivot to one of these next:
- Reverse Integer (LC 7) — same overflow style.
- Valid Number (LC 65) — full state machine for number formats.
- What if we needed to accept scientific notation?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why the explicit phase separation?
Because the spec is a state machine: whitespace → sign → digits → ignore-rest. Coding it as one loop with mixed conditions invites the edge-case bugs Apple is testing for.
Is regex really wrong?
Not wrong, but Apple wants to see manual parsing for the same reason atoi exists in C — the question is about handling the input character by character with explicit overflow checks.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill String to Integer (atoi) and other Apple interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →