🧠
Recursive Digit Extraction
💡 This approach uses recursion to reverse digits by peeling off digits from the end and building the reversed number during the recursion unwind. It is less common but helps understand recursion with integer math.
Intuition
Recursively extract the last digit and build the reversed number by passing an accumulator through recursive calls.
Algorithm
- Define a helper recursive function with current number and accumulator.
- Base case: if number is 0, return accumulator.
- Extract last digit using modulo 10.
- Check if appending digit causes overflow.
- Recurse with number divided by 10 and updated accumulator.
- Return final reversed number with sign.
💡 The challenge is managing accumulator and overflow checks correctly in recursion.
def reverse(x: int) -> int:
INT_MAX = 2**31 - 1
INT_MIN = -2**31
sign = -1 if x < 0 else 1
x = abs(x)
def helper(num, rev):
if num == 0:
return rev
pop = num % 10
if rev > INT_MAX // 10 or (rev == INT_MAX // 10 and pop > 7):
return 0
return helper(num // 10, rev * 10 + pop)
rev = helper(x, 0)
return sign * rev if rev != 0 else 0
# Driver code
if __name__ == '__main__':
test_cases = [123, -123, 120, 1534236469]
for tc in test_cases:
print(f"Input: {tc} -> Output: {reverse(tc)}")
Line Notes
sign = -1 if x < 0 else 1Store sign for later reapplication.
x = abs(x)Work with absolute value for digit extraction.
def helper(num, rev):Recursive helper function with current number and reversed accumulator.
if num == 0:Base case: no more digits to process.
pop = num % 10Extract last digit.
if rev > INT_MAX // 10 or (rev == INT_MAX // 10 and pop > 7)Check for overflow before appending digit.
return helper(num // 10, rev * 10 + pop)Recurse with next digit and updated reversed number.
return sign * rev if rev != 0 else 0Return reversed number with sign or 0 if overflow.
public class Solution {
private static final int INT_MAX = Integer.MAX_VALUE;
private static final int INT_MIN = Integer.MIN_VALUE;
public int reverse(int x) {
int sign = x < 0 ? -1 : 1;
x = Math.abs(x);
int rev = helper(x, 0);
return rev == 0 ? 0 : sign * rev;
}
private int helper(int num, int rev) {
if (num == 0) return rev;
int pop = num % 10;
if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && pop > 7)) return 0;
return helper(num / 10, rev * 10 + pop);
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] testCases = {123, -123, 120, 1534236469};
for (int tc : testCases) {
System.out.println("Input: " + tc + " -> Output: " + sol.reverse(tc));
}
}
}
Line Notes
int sign = x < 0 ? -1 : 1;Store sign for later.
x = Math.abs(x);Work with absolute value.
int rev = helper(x, 0);Call recursive helper with initial accumulator 0.
if (num == 0) return rev;Base case for recursion.
int pop = num % 10;Extract last digit.
if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && pop > 7)) return 0;Check overflow before appending.
return helper(num / 10, rev * 10 + pop);Recursive call with updated values.
return rev == 0 ? 0 : sign * rev;Return reversed number with sign or 0 if overflow.
#include <iostream>
#include <climits>
using namespace std;
int helper(int num, int rev) {
if (num == 0) return rev;
int pop = num % 10;
if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && pop > 7)) return 0;
return helper(num / 10, rev * 10 + pop);
}
int reverse(int x) {
int sign = x < 0 ? -1 : 1;
x = abs(x);
int rev = helper(x, 0);
return rev == 0 ? 0 : sign * rev;
}
int main() {
int testCases[] = {123, -123, 120, 1534236469};
for (int tc : testCases) {
cout << "Input: " << tc << " -> Output: " << reverse(tc) << endl;
}
return 0;
}
Line Notes
if (num == 0) return rev;Base case for recursion.
int pop = num % 10;Extract last digit.
if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && pop > 7)) return 0;Check overflow before appending.
return helper(num / 10, rev * 10 + pop);Recursive call with updated values.
int sign = x < 0 ? -1 : 1;Store sign for later.
x = abs(x);Work with absolute value.
int rev = helper(x, 0);Call recursive helper.
return rev == 0 ? 0 : sign * rev;Return reversed number with sign or 0 if overflow.
var reverse = function(x) {
const INT_MAX = 2**31 - 1;
const sign = x < 0 ? -1 : 1;
x = Math.abs(x);
function helper(num, rev) {
if (num === 0) return rev;
let pop = num % 10;
if (rev > Math.floor(INT_MAX / 10) || (rev === Math.floor(INT_MAX / 10) && pop > 7)) return 0;
return helper(Math.floor(num / 10), rev * 10 + pop);
}
const rev = helper(x, 0);
return rev === 0 ? 0 : sign * rev;
};
// Test cases
[123, -123, 120, 1534236469].forEach(tc => {
console.log(`Input: ${tc} -> Output: ${reverse(tc)}`);
});
Line Notes
const sign = x < 0 ? -1 : 1;Store sign for later.
x = Math.abs(x);Work with absolute value.
function helper(num, rev)Recursive helper function with accumulator.
if (num === 0) return rev;Base case for recursion.
let pop = num % 10;Extract last digit.
if (rev > Math.floor(INT_MAX / 10) || (rev === Math.floor(INT_MAX / 10) && pop > 7)) return 0;Check overflow before appending.
return helper(Math.floor(num / 10), rev * 10 + pop);Recursive call with updated values.
return rev === 0 ? 0 : sign * rev;Return reversed number with sign or 0 if overflow.
TimeO(d) where d is number of digits
SpaceO(d) due to recursion stack
Each digit is processed once, but recursion adds stack overhead proportional to digits.
💡 For a 10-digit number, recursion depth is 10, which is manageable but less efficient than iteration.
Interview Verdict: Accepted but less preferred
Recursion shows understanding but risks stack overflow and is less efficient than iterative approach.