FastPrepString to Integer (atoi)
Problems/String to Integer (atoi)

String to Integer (atoi)

Microsoft logoMicrosoft● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was the second of two technical interviews after an online assessment in an on-campus Microsoft software engineering internship process, per a review. The review compares its string question to the well-known String to Integer (atoi) problem without giving rules. It says the interviewer looked for dry runs, a list of tests, and attention to unusual inputs.

Here the common atoi-style reading is used. Skip leading spaces, read at most one sign, read digits up to the first other character, and return 0 when there are none. As our own rule, results outside the signed 32-bit range are clamped. s has 0 to 200 characters drawn from letters, digits, spaces, +, - and the period.

Our version is about 80% the same as the reported question.

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:

  1. Skip any spaces at the start.
  2. If the next character is + or -, read it as the sign. Only one sign is allowed; with no sign the number is positive.
  3. 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.
  4. If the number is below -2^31, return -2^31; if it is above 2^31 - 1, return 2^31 - 1.

Everything after the digits is ignored. Return the result.

Function

myAtoi(s: String) → int

Examples

Example 1

s = "   -0042xyz"return = -42

Skip 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 = 0

There 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 = 2147483647

The 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 <= 200
  • s contains 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 SWE Intern Interviews: 244 Experiences (Part 1 of 6)

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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int myAtoi(String s) {
        // Write your code here.
    }
}
Checking account…