8. String to Integer (atoi)
mediumAsked at IntelImplement atoi without using the language's parseInt. Intel loves this question because it's pure edge-case discipline — leading whitespace, optional sign, overflow clamping, non-digit termination. The rubric grades whether you handle every edge case before writing the happy-path loop.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Intel loops.
- Glassdoor (2026-Q1)— Intel SWE-II onsite reports cite atoi as a recurring edge-case-discipline round.
- GeeksforGeeks (2025-09)— Intel Interview Experience archives explicitly call out atoi with overflow + sign edge cases.
- LeetCode discuss (2025-12)— Intel-tagged in the LC company-frequency listing.
Problem
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: (1) Read in and ignore any leading whitespace. (2) Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present. (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, '0032' -> 32). If no digits were read, then the integer is 0. (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. Specifically, integers less than -2^31 should be clamped to -2^31, and integers greater than 2^31 - 1 should be clamped to 2^31 - 1. (6) 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.
Approaches
1. Five-phase state machine (canonical)
Phase 1: skip leading whitespace. Phase 2: read optional sign. Phase 3: read digits, accumulating result with overflow pre-check. Phase 4: stop at non-digit. Phase 5: apply sign and clamp.
- Time
- O(n) where n = length of s
- Space
- O(1)
function myAtoi(s) {
const INT_MAX = 2 ** 31 - 1; // 2147483647
const INT_MIN = -(2 ** 31); // -2147483648
let i = 0;
const n = s.length;
// Phase 1: leading whitespace
while (i < n && s[i] === ' ') i++;
if (i === n) return 0;
// Phase 2: sign
let sign = 1;
if (s[i] === '+') { i++; }
else if (s[i] === '-') { sign = -1; i++; }
// Phase 3-4: digits with overflow pre-check
let result = 0;
while (i < n && s[i] >= '0' && s[i] <= '9') {
const digit = s.charCodeAt(i) - 48; // '0' = 48
// Pre-overflow check (using INT_MAX since we apply sign at end)
if (result > Math.floor(INT_MAX / 10) || (result === Math.floor(INT_MAX / 10) && digit > 7)) {
return sign === 1 ? INT_MAX : INT_MIN;
}
result = result * 10 + digit;
i++;
}
// Phase 5: apply sign
return sign * result;
}Tradeoff: Single pass, constant space, explicit overflow handling. The state-machine framing makes the 'forget the leading whitespace' or 'forget the overflow' bug impossible because each phase is a distinct block.
Intel-specific tips
Intel reviewers will explicitly throw nasty inputs at you: leading spaces, '+', '-', '00', '0-1', 'words and 987', integer overflow ('91283472332'). The senior signal is talking through ALL these BEFORE coding: 'I'll do skip-whitespace, optional-sign, digit-accumulate-with-pre-check, return-with-sign. If any phase fails, return what I've accumulated so far (or 0 if nothing).' The pre-overflow check using INT_MAX/10 + digit > 7 is the Intel-flavored 'don't multiply if it would overflow' pattern.
Common mistakes
- Doing the overflow check AFTER the multiply — in C/C++ this is undefined behavior; in JS it gives a wrong number for INT_MIN clamping.
- Using parseInt or Number() — defeats the point.
- Forgetting to clamp on overflow — returning the overflowed value is a common bug.
- Mishandling the empty/whitespace-only string — should return 0, not crash.
- Treating multiple signs ('++') or sign-after-digit ('1+1') as legal — only one leading sign is allowed.
Follow-up questions
An interviewer at Intel may pivot to one of these next:
- Reverse Integer (LC 7) — same overflow-checking discipline.
- Valid Number (LC 65) — fuller state machine including decimals and scientific notation.
- Add Strings (LC 415) — big-integer addition; no overflow because the result is also a string.
- What if the input is UTF-8 with leading BOM? (Skip the BOM byte sequence in phase 1.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why a state machine framing instead of one big loop?
Each phase has a distinct end condition (whitespace ends on first non-space; sign reads at most one char; digit phase reads while digit). Mixing them into one loop forces branching on every char, making the code error-prone. The phase structure mirrors how real lexers are written.
Why does the pre-overflow check use `digit > 7`?
INT_MAX = 2147483647. INT_MAX / 10 = 214748364. The 'next digit' that pushes us over INT_MAX is 8 or higher (since 214748364 * 10 + 8 = 2147483648 > INT_MAX). For the negative side INT_MIN = -2147483648, the symmetric check is digit > 8, but we apply sign at the end so we use the INT_MAX threshold.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill String to Integer (atoi) and other Intel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →