String to Integer (atoi)
Updated
Problem statement
Convert the leading decimal integer in the ASCII string text into a signed 32-bit integer.
- Skip leading ASCII whitespace: space, tab, line feed, vertical tab, form feed, or carriage return.
- Read at most one optional
+or-sign. - Read the immediately following consecutive digits. Stop at the first nondigit; ignore the remaining suffix.
- Return zero if no digits were read. If the value is below
-2147483648, return-2147483648. If it is above2147483647, return2147483647.
Only the leading digit run is parsed; decimal points, exponents, and later digit runs are not special syntax.
Function
parseAtoi(text: String) → intExamples
Example 1
text = " -42tail"return = -42Skip spaces, read the minus sign and digits 42, and stop before the suffix.
Example 2
text = "4193 with words"return = 4193Parsing stops at the first space after the digits.
Example 3
text = "words 987"return = 0The 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