Reverse Integer Solution for Accenture

Company: Accenture · Difficulty: Medium · Topics: Math

Problem Statement

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Key Understanding:

Examples

Example 1:

Input: x = 123

Output: 321

Explanation:

Example 2:

Input: x = -123

Output: -321

Explanation:

Example 3:

Input: x = 120

Output: 21

Explanation:

Example 4:

Input: x = 1534236469

Output: 0

Explanation:

Example 5:

Input: x = 0

Output: 0

Explanation:

Intuition

The key insight is to extract digits one by one and build the reversed number. We need to handle overflow carefully by checking before multiplying by 10.

Why This Approach Works:

Overflow Detection Strategy:

Approach

Approach 1: Mathematical with Overflow Detection (Optimal)

Algorithm:

1. Handle edge case: if x = 0, return 0

2. Extract last digit using modulo: digit = x % 10

3. Check for overflow before multiplying result by 10

4. Build reversed number: result = result * 10 + digit

5. Remove last digit: x = x / 10

6. Repeat until x = 0

7. Apply original sign

Overflow Check:

Approach 2: String Conversion

Algorithm:

1. Convert integer to string

2. Reverse the string (excluding sign)

3. Convert back to integer

4. Handle overflow using long long

5. Apply sign

Approach 3: Power-based Approach

Algorithm:

1. Count number of digits

2. Use powers of 10 to reverse

3. Check for overflow at each step

4. Apply sign

Dry Run

Let's trace through Example 1: x = 123

Step-by-Step Process:

1. Initial: x = 123, result = 0

2. Step 1: digit = 123 % 10 = 3

3. Step 2: digit = 12 % 10 = 2

4. Step 3: digit = 1 % 10 = 1

5. Result: 321

Overflow Example: x = 1534236469

1. Step 1: digit = 9, result = 9

2. Step 2: digit = 6, result = 96

3. ... continues until overflow occurs

4. Overflow detected: return 0

Key Insights

1. Modulo Operation: x % 10 gives the last digit

2. Integer Division: x / 10 removes the last digit

3. Overflow Prevention: Check before multiplication, not after

4. Sign Handling: Work with absolute value, apply sign at end

5. Leading Zeros: Automatically handled by integer arithmetic

6. Edge Cases: 0, single digits, negative numbers

Edge Cases

1. x = 0: Return 0

2. Single digit: Return the same number

3. Negative numbers: Handle sign separately

4. Overflow cases: Return 0

5. Large numbers: Careful overflow detection

6. Numbers ending in 0: Leading zeros are ignored

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Mathematical with Overflow Detection (Optimal)

Time Complexity: O(log n) - Number of digits

Space Complexity: O(1) - Constant space

Solution 2: String Conversion

Time Complexity: O(log n) - String operations

Space Complexity: O(log n) - String storage

Solution 3: Power-based Approach

Time Complexity: O(log n) - Multiple passes

Space Complexity: O(1) - Constant space

More Accenture coding questions