Reverse an Integer Safely
Talk it through out loud with your AI coach.
The problem
- medium
- Pro
Reverse the digits of a signed 32-bit integer, so one hundred and twenty-three becomes three hundred and twenty-one and minus one hundred and twenty becomes minus twenty-one. If the reversed value doesn't fit in a signed 32-bit integer, return zero. Assume you can't store any number bigger than 32 bits.
What a strong answer covers
- Taking digits off one number and building them onto another.
- How negative numbers are dealt with.
- Spotting an overflow before it happens, not after.
- The range involved and the cost.
Common mistake: Building the reversed number first and checking the range afterwards, when a fixed-width value has already wrapped around.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud