As reported: Microsoft SDE-2 (Nov 2025), round 4 (AA, with a very senior interviewer), after past-experience questions: implement C++
stoi, with a discussion of the edge cases.
Implement myAtoi(s), which converts a string to a 32-bit signed integer:
- Skip leading spaces.
- Read an optional
'+'or'-'. - Read digits until a non-digit or the end; ignore the rest.
- No digits →
0. - Clamp to
[−2^31, 2^31 − 1]on overflow.
This is LeetCode 8 — String to Integer (atoi).
Example 1
s = " -42" -> -42
Example 2
s = "4193 with words" -> 4193
Example 3
s = "-91283472332" -> -2147483648
Constraints: 0 ≤ s.length ≤ 200; s contains letters, digits, ' ', '+', '-' and '.'.
Clarifications
- Real
std::stoithrowsinvalid_argumentwhen there are no digits andout_of_rangeon overflow; this version returns0and clamps instead.
Code runner format: one JSON value per line: s (LeetCode's test-case format). Implement myAtoi(s); the harness prints the result.