Reverse an Integer
Updated
Problem statement
Reverse the decimal digits of an integer, positive or negative, without converting the integer to a string or using string manipulation.
Complete reverse(int x). Preserve the sign, discard leading zeros in the reversed digits, and return 0 if the reversed value falls outside the signed 32 bit integer range.
Function
reverse(x: int) → intExamples
Example 1
x = 123return = 321
The reversed integer is 321. Since the reversed integer is within the signed 32-bit integer range, it is the return value.
Example 2
x = 120return = 21
The reversed integer is 21. Notice that the leading zero in the input integer is not present in the reversed integer.
Example 3
x = -120return = -21
The reversed integer is -21. The negative sign is preserved during the reversal.
Constraints
-2^31 <= x <= 2^31 - 1
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)