String to Integer (atoi)
Updated
Problem statement
Convert the string s to a 32-bit signed integer, reading it in the style of atoi with the rules below. The last rule, clamping out-of-range values, is part of this problem's contract:
- Skip any spaces at the start.
- If the next character is
+or-, read it as the sign. Only one sign is allowed; with no sign the number is positive. - Read digits until a character that is not a digit, or the end of the string. Leading zeros are fine. If no digit was read, the result is
0. - If the number is below
-2^31, return-2^31; if it is above2^31 - 1, return2^31 - 1.
Everything after the digits is ignored. Return the result.
Function
myAtoi(s: String) → intExamples
Example 1
s = " -0042xyz"return = -42Skip the three spaces, read the sign -, then read the digits 0042 and stop at x. Leading zeros do not matter, so the result is -42.
Example 2
s = "word 123"return = 0There are no leading spaces, and the first character, w, is neither a sign nor a digit. No digits are read, so the result is 0.
Example 3
s = "99999999999"return = 2147483647The digits form 99,999,999,999, which is larger than 231 - 1 = 2,147,483,647, so the result is clamped to 2147483647.
Constraints
0 <= s.length <= 200scontains only English letters, digits, spaces,+,-and..
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Parse Nested HTML into a TreeApple · ONSITE INTERVIEWApple interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)