The problem. Implement myAtoi(s), which turns a string into a 32-bit signed integer the way C's atoi does:
1) skip leading spaces; 2) read an optional + or -; 3) read digits until the first non-digit (leading zeros are fine); 4) if no digits were read, the result is 0; 5) if the number is outside the 32-bit range [-2³¹, 2³¹ - 1], clamp it to the nearest end.
"42" -> 42
" -042abc" -> -42
"1337c0d3" -> 1337 (stops at "c")
"words and 987" -> 0 (a letter before any digit)
"-91283472332" -> -2147483648 (too small: clamped to -2^31)Parsing is scan-and-build at its most careful. The input is read in stages — spaces, then at most one sign, then digits — and each stage hands over to the next at the first character it doesn't accept. The number is built digit by digit: value = value * 10 + digit.
The only hard part is overflow. The digits can go on far past what any integer type holds ("999999999999999999999"). Either keep the value in a wider type and stop as soon as it passes the limit, or — the version interviewers look for — check before each step whether value * 10 + digit would pass the limit, so the value never overflows at all.
Build the value in a type bigger than 32 bits (a 64-bit long, a JavaScript number, a Python integer), stop reading once it is already past the limit, and clamp at the end.
function myAtoi(s) { const MAX = 2 ** 31 - 1, MIN = -(2 ** 31); let i = 0; while (i < s.length && s[i] === " ") i++; // 1. skip leading spaces let sign = 1; if (s[i] === "+" || s[i] === "-") { // 2. an optional sign if (s[i] === "-") sign = -1; i++; } let value = 0; // a JS number is exact far past 2^31 while (i < s.length && s[i] >= "0" && s[i] <= "9") { // 3. digits, until anything else value = value * 10 + (s.charCodeAt(i) - 48); if (value > MAX + 1) break; // already too big: stop reading i++; } return Math.max(MIN, Math.min(MAX, sign * value)); // 4. clamp into the 32-bit range}Time O(n), space O(1). One pass, and the early stop keeps the wider value from overflowing too. Some interviewers will say assume you only have 32-bit integers — which rules this out.
With only 32-bit integers, test the next step first: value * 10 + d passes MAX exactly when value > (MAX - d) / 10 (rounded down). If it would, return MAX or MIN straight away. A negative number that reaches exactly 2³¹ also returns MIN, which is correct: -2³¹ is the smallest value.
Spaces, then a sign, then digits — with an overflow check before every digit. Try the number that's too big.
function myAtoi(s) { const MAX = 2147483647, MIN = -2147483648; let i = 0; while (i < s.length && s[i] === " ") i++; // 1. skip leading spaces let sign = 1; if (s[i] === "+" || s[i] === "-") { // 2. an optional sign if (s[i] === "-") sign = -1; i++; } let value = 0; while (i < s.length && s[i] >= "0" && s[i] <= "9") { // 3. digits, until anything else const d = s.charCodeAt(i) - 48; // Would value * 10 + d pass MAX? Check first, so nothing ever overflows. if (value > Math.floor((MAX - d) / 10)) return sign === 1 ? MAX : MIN; value = value * 10 + d; i++; } return sign * value;}Time O(n), space O(1). The same single pass, using nothing wider than the answer's own type.
Both are linear; approach 2 is the one that survives the 32-bit only follow-up. Either way, most of the marks are for the edge cases — list them before you code.
"+", "-x": 0."+-12": only one sign is allowed, so 0."- 12": the space isn't a digit, so 0."0000042" is 42."2147483647" and "-2147483648" are fine; one more in either direction clamps.