Manually Parse a Bounded Integer
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Parse the string `text` manually and return a signed 32-bit integer using these rules:
1. Advance past any ASCII spaces (`U+0020`) at the beginning.
2. Read one optional sign, `+` or `-`, immediately after those spaces. If there is no sign, use a positive sign.
3. From the current position, read consecutive decimal digits. Stop at the first character outside `0` through `9`, or when the string ends. Leading zeroes do not change the value. Once reading stops, do not resume at a later digit.
4. If this process reads no digits, return `0`. Otherwise, apply the sign to the number represented by the digit run.
5. Clamp that value to the inclusive range `[-2^31, 2^31 - 1]`: values below the lower endpoint become `-2147483648`, and values above the upper endpoint become `2147483647`.
Perform the numeric conversion yourself. Do not use a built-in string-to-number parser or conversion function, such as `atoi`, `stoi`, `Integer.parseInt`, `int()`, `parseInt`, or `Number()`.
### Examples
**Example 1**
```text
Input: text = " -00073trail9"
Output: -73
```
The initial spaces are skipped, the minus sign is read, and the digit run is `00073`. Reading ends at `t`; the final `9` is never considered.
**Example 2**
```text
Input: text = "+2147483658.4"
Output: 2147483647
```
The digit run ends at the period. Its positive value exceeds the allowed maximum, so the result is clamped to `2147483647`.
### Constraints
- `0 <= text.length <= 200`
- The string may contain uppercase or lowercase English letters, decimal digits, ASCII space (` `), `+`, `-`, and `.`.
Overview: Manually parse a string into a signed 32-bit integer, preserving leading-space, sign, digit-run, stopping and clamping rules without built-in numeric parsers.