8. String to Integer (atoi)
mediumImplement atoi — parse a string into a 32-bit signed integer following the full whitespace/sign/digit/overflow rules. The textbook 'spec-reading' problem.
By Sam K., Founder, InterviewChamp.AI · Last verified
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. Whitespace: Ignore any leading whitespace (' '). 2. Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present. 3. Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. 4. Rounding: If the integer is out of the 32-bit signed integer range [-2^31, 2^31 - 1], then round the integer to remain in the range. Specifically, integers less than -2^31 should be rounded to -2^31, and integers greater than 2^31 - 1 should be rounded to 2^31 - 1. 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 = " -042"-42Example 3
s = "1337c0d3"1337Example 4
s = "0-1"0Example 5
s = "words and 987"0Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
Three pointer-style phases: skip leading whitespace, read optional sign, read digits.
Hint 2
Stop at the first non-digit. Don't parse anything that comes after.
Hint 3
While reading digits, check for overflow BEFORE multiplying by 10. If result > INT_MAX/10 or (result == INT_MAX/10 and digit > 7), clamp to INT_MAX. Mirror for negative.
Hint 4
Edge cases: empty string, all whitespace, only a sign, sign followed by non-digit, leading zeros.
Solution approach
Reveal approach
Three-phase parser. Phase 1: index i = 0; while i < n and s[i] == ' ', advance i. Phase 2: detect sign — if s[i] is '-' set sign = -1 and advance; if '+' just advance; else sign = 1. Phase 3: read digits — result = 0; while i < n and s[i] is a digit: digit = s[i] - '0'; check overflow guard: if result > INT_MAX / 10 OR (result == INT_MAX / 10 AND digit > 7), return sign == 1 ? INT_MAX : INT_MIN; else result = result * 10 + digit; advance i. After the loop return sign * result. The overflow check before multiplying is the canonical defense against the 'cast to wide type' trick. O(n) time, O(1) space.
Complexity
- Time
- O(n)
- Space
- O(1)
Related patterns
- math
- string-scan
- state-machine
Related problems
- 7. Reverse Integer
- 65. Valid Number
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Amazon
- Microsoft
- Apple
- Bloomberg
More Math practice problems
- 7. Reverse Integer
- 9. Palindrome Number
- 12. Integer to Roman
- 13. Roman to Integer
- 29. Divide Two Integers
- 38. Count and Say
- 43. Multiply Strings
- 50. Pow(x, n)