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

String to Integer (atoi)

Apple logoApple● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple new grad phone screen. The report asks for atoi in C. Our version is about 70% the same as the reported question. We chose the ASCII parsing rules, clamping to a signed 32-bit number, and the input limits.

Convert the leading decimal integer in the ASCII string text into a signed 32-bit integer.

  1. Skip leading ASCII whitespace: space, tab, line feed, vertical tab, form feed, or carriage return.
  2. Read at most one optional + or - sign.
  3. Read the immediately following consecutive digits. Stop at the first nondigit; ignore the remaining suffix.
  4. Return zero if no digits were read. If the value is below -2147483648, return -2147483648. If it is above 2147483647, return 2147483647.

Only the leading digit run is parsed; decimal points, exponents, and later digit runs are not special syntax.

Function

parseAtoi(text: String) → int

Examples

Example 1

text = "   -42tail"return = -42

Skip spaces, read the minus sign and digits 42, and stop before the suffix.

Example 2

text = "4193 with words"return = 4193

Parsing stops at the first space after the digits.

Example 3

text = "words 987"return = 0

The leading non-whitespace character is not a sign or digit.

Constraints

  • 0 <= text.length <= 2000.
  • Every input character is ASCII.

More Apple Phone Screen questions

  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
  • Evaluate DivisionPHONE SCREEN · Seen Nov 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
  • Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
  • Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
  • Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
  • Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
  • Integer to English Words in USD and INR StylesVeeam · PHONE SCREEN
  • Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
  • Basic Calculator IISnap Inc. · PHONE SCREENSnap Inc. interview questions
  • Bit at an Index After Repeated Binary ExpansionAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
  • Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
  • Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
  • Evaluate a Flat Expression or Report Invalid InputMeta · PHONE SCREENMeta interview questions
  • Find Accounts with Negative Converted BalancesStripe · PHONE SCREENStripe interview questions

Interview reports that mention this question

  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE Interviews: 37 Experiences (Part 1 of 3)

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int parseAtoi(String text) {
        // Write your solution here.
    }

}
Checking account…