Palindrome Number Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: Math

Problem Statement

Given an integer x, return true if x is a palindrome, and false otherwise.

Key Understanding:

Examples

Example 1:

Input: x = 121

Output: true

Explanation:

Example 2:

Input: x = -121

Output: false

Explanation:

Example 3:

Input: x = 10

Output: false

Explanation:

Example 4:

Input: x = 12321

Output: true

Explanation:

Example 5:

Input: x = 0

Output: true

Explanation:

Intuition

The key insight is that we can check if a number is a palindrome by comparing it with its reverse. However, we can optimize this by only reversing half of the number.

Core Concept:

Why Reverse Half Works:

Approach

Approach 1: Reverse Half (Optimal)

Algorithm:

1. Handle edge cases:

2. Initialize reversed_num = 0

3. While x > reversed_num:

4. Check if x == reversed_num (even length) or x == reversed_num / 10 (odd length)

5. Return true if either condition is met

Why This Works:

Complexity Analysis:

Approach 2: Convert to String

Algorithm:

1. Convert the number to a string

2. Use two pointers: left at start, right at end

3. While left < right:

4. Return true if all characters match

Why This Works:

Complexity Analysis:

Approach 3: Full Reverse

Algorithm:

1. Handle negative numbers (return false)

2. Reverse the entire number

3. Compare the reversed number with the original

4. Return true if they are equal

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: x = 121

Reverse Half Approach:

Initial State: x = 121, reversed_num = 0

Step 1: x = 121, reversed_num = 0

Step 2: x = 12, reversed_num = 1

Step 3: x = 1, reversed_num = 12

Step 4: Check conditions

String Approach:

Step 1: Convert to string

Step 2: Two pointer comparison

Full Reverse Approach:

Step 1: Reverse the number

Step 2: Compare

Key Insights

1. Half Reversal: Only need to reverse half the number for efficiency

2. Negative Numbers: Always false due to minus sign

3. Trailing Zeros: Numbers ending with 0 (except 0) are never palindromes

4. Odd vs Even Length: Handle middle digit differently for odd-length numbers

5. Early Termination: Stop when reversed half >= remaining half

Edge Cases

1. Negative Numbers: Always return false

2. Zero: Special case - is a palindrome

3. Single Digit: Always a palindrome

4. Numbers Ending with 0: Never palindromes (except 0)

5. Large Numbers: Handle integer overflow in reversal

6. Odd vs Even Length: Different comparison logic needed

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: Reverse Half (Optimal)

Time Complexity: O(log n) - We process half the digits

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: Convert to String

Time Complexity: O(log n) - We convert to string and compare

Space Complexity: O(log n) - We need string storage

Advantages:

Disadvantages:

Solution 3: Full Reverse

Time Complexity: O(log n) - We reverse all digits

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

More Accenture coding questions