The power of trying it your own, problem analysis, and debuggers and IDEs

This is a summary for my whole journey of solving the 7. Reverse Integer problem. My purpose from that is to just reflect the power of the simple straightforward intuitive, yet not so many people follow, approach of trying things on your own first, and resisting the temptation to directly see the solutions, and fall into the illusions of comptence of learning (thank you very much Barbara Oakley).

At first, the problem seemed easy and I solved it this way at first:

public int reverse(int x) {
	String reversedStr = "";
    while (x > 0) {
    	reversedStr += x % 10;
        x /= 10;
    }
        
    try {
		return Integer.parseInt(reversedStr);
	} catch (Exception e) {
		return 0; //return 0 if the integer overflows
	}
}

But it turns way that this code has a problem with negative int input. Just using my mind and a hand tracing, I knew that the problem was with the loop continuation condition breaks for negative cases, so I changed the loop continuation condition to be:

public int reverse(int x) {
		String reversedStr = "";

		while (Math.abs(x)> 0) {
			reversedStr += x % 10;
			x /= 10;
		}

		try {
			return Integer.parseInt(reversedStr);
		} catch (Exception e) {
			return 0; //return 0 if the integer overflows
		}
}

However this version has also a problem for negative cases as it jumps to the exception and returns 0. I couldn't figure out the problem at all using just my mind and the traditional hand trace method.* Here when Debuggers and the power of ID clearly appear and come to play.*
So, I used the powerful eclipse debugger and figured out the problem exactly by noticing how values of my variables change so I can spot the error. Btw, I put a breakpoint on the negative test case (-123) and used step into so I can step into my method and see the error. So I figured out the error, and handle it successfully within seconds!!!

Okay, my above solution(which I has submitted) is right but there's an efficient way to solve it since my solution algorithm has a time complexity of O(n). Also, my algorithm of using Strings force me to do extra unnecessary iterations, since my reverse number could exceed int capacity, that is, cause overflow, earlier.
Honestly, I was thinking about not using Strings and just doing it mathematically, (as dealing with objects take much more than dealing with primitives), and I was remembering Khan academy lessons(haha) on place value and regrouping, but after a brainstorm, I still couldn't figure it out.
The provided solution algorithm solves all of that issue and gave me the mathy solution that I was struggling to find. Its much more efficient, since its time complexity is (O(log(x))(There are roughly log10 (x) digits in x. For example, in x = 123, log(x) which is the same as 10^y = 123 ==> 10^y =(rounding) 100 ==> y = 2. So, there are only 2 iterations for the input 123 (not a big deal. No, its a big deal, because when the input gets larger(I mean in length), the number of iterations is less (remember algorithm growth curve), Not like your solution, which is the number of iterations always equals the length of the input, that is, O(n) or linear growth). The worse is, as we said, my solution does not skip unneccasry iterations, which can be skipped earlier. as your rev might overflow earlier.

Phew!! that was a lot of brainstorming!!!!! I feel a bit tired(jk)
The takeaway lesson is that you need to master the skill of start doing it your own(Thanks Barbala Oakley for the illusions of learning stuff). You need to able to deconstruct and analyse solutions so that you spot the strengths and weaknesses of your solutions, and therefore, maintain them.

Comments (1)