564. Find the Closest Palindrome
Asked at Uber
Difficulty
Hard
Time
O(L)
Space
O(L)
Pattern
Math
Companies
1
Problem
You are given a string n that holds a positive integer written in decimal, with no leading zeros. Return the closest palindrome to that number, as a string.
"Closest" means the smallest absolute difference. The number n itself is not allowed as an answer, even if it is already a palindrome — you must return a different number. If two palindromes are equally close (one below, one above), return the smaller one.
The answer may have fewer digits or more digits than n. For example, the closest palindrome to 1001 is 999, and the closest palindrome to 999 is 1001.
Find the nearest palindrome
Constraints
- ●
1 <= n.length <= 18 - ●
n consists of only digits - ●
n does not have leading zeros - ●
n represents an integer in the range [1, 10^18 - 1]
Examples
n = "123""121"n = "1""0"Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Uber | Hard | View all Uber questions → |
How to Think About It
Brute force: check n-1, n-2, ... and n+1, n+2, ... until a palindrome is found — too slow for large n.
Key insight: the closest palindrome has the same number of digits or one fewer/more, and is constructed by mirroring the first half.
Generate candidates by mirroring: mirror(n), mirror(n-1), mirror(n+1), mirror(n-99...9), mirror(n+100...0).
Also consider edge cases: 10...01 mirrors to 9...9 (one fewer digit), 99...9 mirrors to 100...001 (one more digit).
At most 9 candidates to check — compare each against n and return the closest, breaking ties by choosing the smaller.
Optimal Approach
Generate up to 9 candidate palindromes by considering mirroring the prefix of n (the first half). Take the prefix as the first ceil(len/2) digits, mirror it to form a palindrome, and also generate palindromes from prefix-1 and prefix+1 to handle cases where the closest palindrome differs in the middle digits. Additionally handle boundary cases like 10...01 (returns 9...9) and 9...9 (returns 10...01). Compare all candidates (excluding n itself) and return the closest, breaking ties by choosing the smaller value. This runs in O(1) since the number of digits is bounded.
Mirror the prefix
What Trips People Up in Real Interviews
Clarify that n itself is never the answer (must return a different palindrome).
Edge cases: n=1 returns "0" (closest palindrome), n=2 returns "1".
The candidate generation must handle carry/borrow when incrementing/decrementing the prefix.
Use string comparison for large numbers since n can be up to 18 digits (exceeds 64-bit).
Tie-breaking: if two candidates are equidistant, return the smaller one.
Solution Code
O(L) timedef nearestPalindromic(n):
length = len(n)
if length == 1:
return str(int(n) - 1)
candidates = set()
prefix = int(n[:(length + 1) // 2])
for p in [prefix - 1, prefix, prefix + 1]:
s = str(p)
if length % 2 == 0:
candidate = s + s[::-1]
else:
candidate = s + s[:-1][::-1]
candidates.add(candidate)
candidates.add('9' * (length - 1))
candidates.add('1' + '0' * (length - 1) + '1')
candidates.discard(n)
best = None
n_int = int(n)
for c in candidates:
diff = abs(int(c) - n_int)
if best is None or diff < abs(int(best) - n_int) or (diff == abs(int(best) - n_int) and int(c) < int(best)):
best = c
return bestFrequently Asked Questions
What is the Find the Closest Palindrome problem?
Given a string n representing an integer, return the closest palindrome (not including itself) to n. If there is a tie, return the smaller one. The input n is a string of length 1 to 18 digits and will not have leading zeros.
How do you solve Find the Closest Palindrome?
The optimal approach runs in O(L) time and O(L) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Find the Closest Palindrome?
Find the Closest Palindrome is asked at Uber. It is a hard difficulty problem.
What are common mistakes on Find the Closest Palindrome?
- Clarify that n itself is never the answer (must return a different palindrome).
- Edge cases: n=1 returns "0" (closest palindrome), n=2 returns "1".
- The candidate generation must handle carry/borrow when incrementing/decrementing the prefix.
- Use string comparison for large numbers since n can be up to 18 digits (exceeds 64-bit).
- Tie-breaking: if two candidates are equidistant, return the smaller one.