Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

8. String to Integer (atoi)

Medium18.3% Acceptance
String
Asked by:
F
Facebook
M
Microsoft
ProblemSolutions (8)VideosCompanies (9)Notes

Problem Statement

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 [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1.

Return the integer as the final result.

Example 1:

Input: s = "42"

Output: 42

Explanation:

The underlined characters are what is read in and the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
         ^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
         ^
Step 3: "42" ("42" is read in)
           ^

Example 2:

Input: s = " -042"

Output: -42

Explanation:

Step 1: "   -042" (leading whitespace is read and ignored)
            ^
Step 2: "   -042" ('-' is read, so the result should be negative)
             ^
Step 3: "   -042" ("042" is read in, leading zeros ignored in the result)
               ^

Example 3:

Input: s = "1337c0d3"

Output: 1337

Explanation:

Step 1: "1337c0d3" (no characters read because there is no leading whitespace)
         ^
Step 2: "1337c0d3" (no characters read because there is neither a '-' nor '+')
         ^
Step 3: "1337c0d3" ("1337" is read in; reading stops because the next character is a non-digit)
             ^

Example 4:

Input: s = "0-1"

Output: 0

Explanation:

Step 1: "0-1" (no characters read because there is no leading whitespace)
         ^
Step 2: "0-1" (no characters read because there is neither a '-' nor '+')
         ^
Step 3: "0-1" ("0" is read in; reading stops because the next character is a non-digit)
          ^

Example 5:

Input: s = "words and 987"

Output: 0

Explanation:

Reading stops at the first non-digit character 'w'.

Constraints:

  • 0 <= s.length <= 200
  • s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
G
Google
B
Bloomberg
+4

Approach

The key idea in String to Integer (atoi) is to simulate how numeric parsing works in most programming languages. Start by scanning the string from left to right and ignoring leading whitespace. After that, determine whether the number is positive or negative by checking for an optional '+' or '-' sign.

Next, process consecutive numeric characters and convert them into an integer by repeatedly updating the result using the digit value. While doing this, it is important to stop parsing when a non-digit character appears. Another crucial detail is handling 32-bit signed integer overflow. Before appending each new digit, check whether the current number would exceed the allowed range and clamp it to the limits if necessary.

This approach uses a single pass over the string and constant extra space. The challenge lies in carefully handling edge cases such as empty strings, strings with only spaces, invalid characters, and overflow conditions.

Complexity

ApproachTime ComplexitySpace Complexity
Single-pass string parsingO(n)O(1)

Video Solution Available

Techdose

View all video solutions

Solutions (8)

Iterative Parsing and Conversion

In this approach, we manually parse the string character by character. First, we skip any leading whitespace. Then, we determine the sign of the number by checking the next character. After we've set up the sign, we convert succeeding string characters to a number as long as they are digits, and check if the number overflows the 32-bit signed integer range. The iterative parsing continues until we encounter a non-numeric character.

Time Complexity: O(n), where n is the length of the string because we traverse the string once.

Space Complexity: O(1), since we only use a fixed amount of extra space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <limits.h>
3
4int myAtoi(char * s) {
5    int i = 0,

Explanation

The function begins by traversing any leading whitespace in the input string. If the next character is a '-' or '+', it sets the sign accordingly and moves to the next character. We update a 'result' variable as we iterate over each digit, checking against overflow conditions. This result is carefully treated as a long to avoid overflow, then cast to an int at the end. Edge cases, including overflow beyond 32-bit bounds, are also managed explicitly.

Regular Expression Approach

Using regular expressions simplifies extracting the integer portion from a string. This involves defining a regular expression to match the pattern for potential number initializations, determining whether followed by valid digits until non-numeric encounters or string end, and then mathematically transforming results. While elegant, care still needs to be taken on handling sign and range boundaries.

Time Complexity: O(n), regular expression parsing inspects the input sequence linearly.

Space Complexity: O(1), denoting the small constant space taken up by the quantitative calculation.

JavaScriptPython
1var myAtoi = function(s) 

Video Solutions

Watch expert explanations and walkthroughs

Implement atoi | Convert string to integer

Techdose
5:5299,664 views

Asked By Companies

9 companies
F
Facebook
M
Microsoft
A
Amazon
G
Google
B
Bloomberg
A
Adobe
G
Goldman Sachs
R
Redfin
A
Apple

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Substring Without Repeating CharactersMedium
Longest Palindromic SubstringMedium
Zigzag ConversionMedium
Regular Expression MatchingHard
More similar problems

Related Topics

String

Problem Stats

Acceptance Rate18.3%
DifficultyMedium
Companies9

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is String to Integer (atoi) asked in FAANG interviews?

Yes, this problem or its variations frequently appear in technical interviews at large tech companies. It tests attention to detail, string parsing logic, and the ability to handle edge cases correctly.

What is the optimal approach for String to Integer (atoi)?

The optimal approach is a single-pass parsing method. You skip leading spaces, detect the optional sign, process digits sequentially, and stop when a non-digit appears while checking for overflow at each step.

Why is overflow handling important in String to Integer (atoi)?

While converting digits into an integer, the value may exceed the 32-bit signed integer range. Proper solutions detect this early and clamp the result to INT_MAX or INT_MIN to avoid incorrect results.

What data structure is best for solving String to Integer (atoi)?

No complex data structure is required for this problem. A few variables to track the current index, sign, and accumulated number are enough while iterating through the string.

Previous Problem

Zigzag Conversion

Next Problem

Regular Expression Matching

sign
=
1
;
6
while
(
s
[
i
]
==
' '
)
i
++
;
7
if
(
s
[
i
]
==
'-'
||
s
[
i
]
==
'+'
)
8
sign
=
(
s
[
i
++
]
==
'-'
)
?
-
1
:
1
;
9
long
result
=
0
;
10
while
(
s
[
i
]
>=
'0'
&&
s
[
i
]
<=
'9'
)
{
11
result
=
result
*
10
+
(
s
[
i
++
]
-
'0'
)
;
12
if
(
result
*
sign
>
INT_MAX
)
return
INT_MAX
;
13
if
(
result
*
sign
<
INT_MIN
)
return
INT_MIN
;
14
}
15
return
(
int
)
(
result
*
sign
)
;
16
}
17
18
int
main
(
)
{
19
printf
(
"%d\n"
,
myAtoi
(
" -42"
)
)
;
20
printf
(
"%d\n"
,
myAtoi
(
"4193 with words"
)
)
;
21
printf
(
"%d\n"
,
myAtoi
(
"words and 987"
)
)
;
22
printf
(
"%d\n"
,
myAtoi
(
"-91283472332"
)
)
;
23
return
0
;
24
}
{
2
const
match
=
s
.
match
(
/
^\s*([+-]?\d+)
/
)
;
3
const
num
=
match
?
parseInt
(
match
[
1
]
,
10
)
:
0
;
4
const
intMax
=
2
**
31
-
1
;
5
const
intMin
=
-
(
2
**
31
)
;
6
if
(
num
>
intMax
)
return
intMax
;
7
if
(
num
<
intMin
)
return
intMin
;
8
return
num
;
9
}
;
10
11
// Example usages:
12
console
.
log
(
myAtoi
(
"-42"
)
)
;
13
console
.
log
(
myAtoi
(
"4193 with words"
)
)
;
14
console
.
log
(
myAtoi
(
"words and 987"
)
)
;
15
console
.
log
(
myAtoi
(
"-91283472332"
)
)
;

Explanation

This JavaScript function utilizes a regular expression to detect any leading spaces, an optional sign, and a sequence of digits at the start of the string. The parseInt utility parses the integer, which is tempered by limits for overflowing numbers using conditional returns.