Reversing Integers with Overflow Guard in Java

Day 62 of my 365-Day DSA Challenge 🔥 Solved LC 7 — Reverse Integer (Medium) | Runtime: 1ms ⚡ But here's the real story --> I got accepted on my first attempt, and then I spotted my own bug. My original solution had no overflow guard. The line:  rev = rev * 10 + original % 10 ...could silently overflow an int, and I wouldn't even know it. LeetCode just happened not to hit that edge case hard enough. The fix? Check BEFORE multiplying: → If rev > MAX_VALUE / 10, it will overflow → return 0 → If rev == MAX_VALUE / 10 and next digit > 7 → return 0 (Same logic for the negative side with MIN_VALUE and -8) This is why understanding WHY your code works matters more than just seeing "Accepted." A passing test ≠ correct code. 365 days. One problem at a time. 💪 #DSA #LeetCode #Java #365DayChallenge #SoftwareEngineering #CodingJourney

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories