FastPrepString to Integer with Clamping
Problems/String to Integer with Clamping

String to Integer with Clamping

AMD logoAMD● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source asks for a str-to-int method in C. Grammar, stop behavior, bounds and portable callable interface are authored; approximate source match 75%. An exact source date is not available.

Skip leading ASCII spaces, then consume at most one optional + or - sign followed immediately by a maximal run of decimal digits. Stop at the first other character and ignore the remaining suffix. Return 0 if no digit was consumed. Clamp results to the signed 32-bit interval [-2147483648,2147483647]. Only the ordinary space character is skipped; interior spaces end digit parsing.

Function

parseInteger(s: String) → int

Examples

Example 1

s = "  -42words"return = -42

Two leading spaces are skipped, the sign is negative, and the digit run is 42 before w.

Example 2

s = "2147483648"return = 2147483647

The positive value exceeds the upper signed32 limit and is clamped to 2147483647.

Constraints

  • 0 <= s.length <= 1000.
  • Every character is printable ASCII with a code from 32 through 126.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public int parseInteger(String s) {
    // Write your code here.
}
Checking account…