Convert a String to a 32-bit Signed Integer with Clamping

Quick Overview

Convert a string to a 32-bit signed integer by skipping leading spaces, reading an optional sign and the digits that follow, and clamping out-of-range values. The problem tests careful handling of parsing edge cases and detecting overflow in fixed-width integer arithmetic.

Convert a String to a 32-bit Signed Integer with Clamping

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Implement a function `my_atoi(s)` that converts a string to a 32-bit signed integer. The conversion works as follows: 1. Ignore any leading whitespace (the space character `' '`). 2. Determine the sign by checking whether the next character is `'-'` or `'+'`. If neither is present, the number is positive. 3. Read the digits that follow, stopping at the first non-digit character or at the end of the string. Leading zeros do not change the value. If no digits were read, the result is `0`. 4. If the integer is outside the 32-bit signed range `[-2^31, 2^31 - 1]`, clamp it to that range. Integers less than `-2^31` (`-2147483648`) become `-2^31`, and integers greater than `2^31 - 1` (`2147483647`) become `2^31 - 1`. Return the integer as the final result. ### Function Signature ```python def my_atoi(s: str) -> int: ``` ### Rules - Only the space character counts as leading whitespace. - At most one sign character is read, and it must come right after the leading spaces. The digits must come right after the sign. Any other character in that position ends parsing with no digits read, so `"+-12"` and `"- 42"` both give `0`. - Everything after the last digit of the first run of digits is ignored, so `"4193 with words"` gives `4193` and `"3.14"` gives `3`. - A string with no digits in that position gives `0`, for example `""`, `" "` and `"words and 987"`. ### Constraints - `0 <= len(s) <= 200` - `s` consists of English letters (lowercase and uppercase), digits (`0-9`), `' '`, `'+'`, `'-'` and `'.'`. ### Examples ```text Input: s = " -042" Output: -42 ``` The three leading spaces are skipped and `'-'` sets the sign. The leading zero adds nothing, and `"42"` is read, giving -42. ```text Input: s = "1337c0d3" Output: 1337 ``` There are no spaces and no sign. The digits `"1337"` are read, and parsing stops at `'c'`, so the later digits are ignored. ```text Input: s = "-91283472332" Output: -2147483648 ``` The value read is -91283472332, which is below `-2^31`, so it is clamped to -2147483648.

Overview: Convert a string to a 32-bit signed integer by skipping leading spaces, reading an optional sign and the digits that follow, and clamping out-of-range values. The problem tests careful handling of parsing edge cases and detecting overflow in fixed-width integer arithmetic.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Implement a function my_atoi(s) that converts a string to a 32-bit signed integer.

The conversion works as follows:

  1. Ignore any leading whitespace (the space character ' ' ).
  2. Determine the sign by checking whether the next character is '-' or '+' . If neither is present, the number is positive.
  3. Read the digits that follow, stopping at the first non-digit character or at the end of the string. Leading zeros do not change the value. If no digits were read, the result is 0 .
  4. If the integer is outside the 32-bit signed range [-2^31, 2^31 - 1] , clamp it to that range. Integers less than -2^31 ( -2147483648 ) become -2^31 , and integers greater than 2^31 - 1 ( 2147483647 ) become 2^31 - 1 .

Return the integer as the final result.

Function Signature

def my_atoi(s: str) -> int:

Rules

  • Only the space character counts as leading whitespace.
  • At most one sign character is read, and it must come right after the leading spaces. The digits must come right after the sign. Any other character in that position ends parsing with no digits read, so "+-12" and "- 42" both give 0 .
  • Everything after the last digit of the first run of digits is ignored, so "4193 with words" gives 4193 and "3.14" gives 3 .
  • A string with no digits in that position gives 0 , for example "" , " " and "words and 987" .

Constraints

  • 0 <= len(s) <= 200
  • s consists of English letters (lowercase and uppercase), digits ( 0-9 ), ' ' , '+' , '-' and '.' .

Examples

Input: s = "   -042"
Output: -42

The three leading spaces are skipped and '-' sets the sign. The leading zero adds nothing, and "42" is read, giving -42.

Input: s = "1337c0d3"
Output: 1337

There are no spaces and no sign. The digits "1337" are read, and parsing stops at 'c', so the later digits are ignored.

Input: s = "-91283472332"
Output: -2147483648

The value read is -91283472332, which is below -2^31, so it is clamped to -2147483648.