Skip to main content

8. String to Integer (atoi)

medium

Implement atoi — parse a string into a 32-bit signed integer following the full whitespace/sign/digit/overflow rules. The textbook 'spec-reading' problem.

By Alex Chen, 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 <= 200
  • s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.

Examples

Example 1

Input
s = "42"
Output
42

Example 2

Input
s = " -042"
Output
-42

Example 3

Input
s = "1337c0d3"
Output
1337

Example 4

Input
s = "0-1"
Output
0

Example 5

Input
s = "words and 987"
Output
0

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

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

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Microsoft
  • Apple
  • Bloomberg

Practice these live with InterviewChamp.AI

Drill String to Integer (atoi) and Math problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →