FastPrepParse a Signed 32-Bit Integer with Overflow Detection

Parse a Signed 32-Bit Integer with Overflow Detection

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a nonempty decimal string s, parse it as a signed 32-bit integer.

The string may begin with one optional + or - sign, followed by one or more decimal digits. It contains no spaces or other characters. Return the canonical decimal representation of the parsed integer. If the value is outside [-2147483648, 2147483647], return "OVERFLOW".

Function

parseInt32(s: String) → String

Examples

Example 1

s = "-2147483648"return = "-2147483648"

The value equals the minimum signed 32-bit integer and is valid.

Example 2

s = "9999999999"return = "OVERFLOW"

The value exceeds the signed 32-bit maximum, so the runner exposes the required overflow exception as OVERFLOW.

Constraints

  • 1 <= s.length <= 100000
  • After an optional leading sign, every character is a decimal digit.

More Microsoft problems

See Microsoft hiring insights
public String parseInt32(String s) {
  // Write your code here.
}
s"-2147483648"
expected"-2147483648"
Checking account…