String to Integer (atoi)
Updated
Problem statement
Convert s to a signed 32-bit integer.
- Skip leading ASCII space characters.
- Read one optional
+or-sign. - Consume the longest following prefix of decimal digits. If no digit is consumed, return
0. - Ignore every character after that digit prefix.
- Clamp a value below
-2^31to-2^31and a value above2^31 - 1to2^31 - 1.
Function
stringToInteger(s: String) → intExamples
Example 1
s = "123"return = 123All characters belong to the decimal digit prefix.
Example 2
s = " -042xyz"return = -42Leading spaces and the negative sign are consumed. Parsing stops before x.
Example 3
s = "91283472332"return = 2147483647The parsed positive value exceeds the signed 32-bit maximum and is clamped.
Constraints
0 <= s.length <= 200.smay contain ASCII letters, digits, spaces, signs, and punctuation.
More Bloomberg LP Phone Screen questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Ordered IP FirewallDatabricks · PHONE SCREENDatabricks interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Minimum Number of Frogs CroakingCerebras · PHONE SCREEN
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Read Across Sound TracksAirbnb · PHONE SCREENAirbnb interview questions
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions