Quick Overview

Given a positive integer written as a digit string of up to 18 characters, find the nearest palindromic integer other than the number itself, with ties going to the smaller value. It tests building a small candidate set from the first half of the number and handling the cases where the digit count changes.

Find the nearest palindromic integer to a numeric string, excluding the number itself

Company: Yelp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given a string `n` that represents a positive integer, return the closest integer to it that is a palindrome, not counting `n` itself. Closest means the absolute difference between the two integers is as small as possible. If two palindromes are equally close, return the smaller one. ### Function Signature ```python def nearest_palindrome(n: str) -> str: ``` ### Rules - Return the answer as a decimal string with no leading zeros, for example `"121"` or `"0"`. - A palindrome is a non-negative integer whose decimal digits read the same forwards and backwards. Every single-digit number, including `0`, is a palindrome. - The answer must differ from the integer `n`, even when `n` is itself a palindrome. - When two palindromes are at the same distance from `n`, return the smaller one. ### Constraints - `1 <= len(n) <= 18` - `n` consists only of digits and has no leading zeros. - `n` represents an integer from `1` to `10^18 - 1`. ### Examples ```text Input: n = "123" Output: "121" ``` 121 is 2 away from 123. The next palindrome above, 131, is 8 away. ```text Input: n = "1" Output: "0" ``` 1 itself is excluded. Both 0 and 2 are 1 away, and the tie goes to the smaller value, 0. ```text Input: n = "100" Output: "99" ``` 99 and 101 are both 1 away from 100, so the smaller one, 99, is returned.

Overview: Given a positive integer written as a digit string of up to 18 characters, find the nearest palindromic integer other than the number itself, with ties going to the smaller value. It tests building a small candidate set from the first half of the number and handling the cases where the digit count changes.

Given a string `n` that holds the decimal digits of a positive integer, return the palindrome closest to that integer, leaving out `n` itself. Closeness is the absolute difference between the two integers. If two palindromes are equally close, return the smaller one. ### Rules - A palindrome is a non-negative integer whose decimal digits read the same forwards and backwards. Every single-digit number, including `0`, is a palindrome. - The answer must differ from the integer `n`, even when `n` is itself a palindrome. - Return the answer as a decimal string with no leading zeros, for example `"121"` or `"0"`. The integers involved can exceed 2^31 - 1 and also 2^53: `n` can be as large as 10^18 - 1, and the answer can be as large as 10^18 + 1 (19 digits). Every value fits in a signed 64-bit integer (`long` in Java, `long long` in C++); JavaScript needs `BigInt` for exact arithmetic. ### Example 1 ```text Input: n = "123" Output: "121" ``` 121 is 2 away from 123. The next palindrome above, 131, is 8 away. ### Example 2 ```text Input: n = "100" Output: "99" ``` 99 and 101 are both 1 away from 100, so the smaller one, 99, is returned. ### Constraints - `1 <= len(n) <= 18` - `n` consists only of digits and has no leading zeros. - `n` represents an integer from `1` to `10^18 - 1`.

Constraints

  • 1 <= len(n) <= 18
  • n consists only of digits and has no leading zeros.
  • n represents an integer from 1 to 10^18 - 1.

Examples

Input: ('1',)

Expected Output: '0'

Explanation: Source example: 1 itself is excluded, 0 and 2 tie at distance 1, and the smaller 0 wins.

Input: ('9',)

Expected Output: '8'

Explanation: Largest single digit: 8 is 1 away, 10 is not a palindrome, and 11 is 2 away.

Hints

  1. Values reach 10^18 + 1, past the range where a double-precision float is exact, so keep every comparison exact (64-bit integers, or BigInt in JavaScript).
  2. The examples 100 and 1 show that the closest palindrome can have fewer digits than n. Consider when it could have more digits.
  3. n itself never counts, so a palindromic n still needs the nearest other palindrome on each side, and a tie goes to the smaller value.

Closest Palindrome Other Than the Number Itself

hardYelpSoftware Engineer

Given a string n that holds the decimal digits of a positive integer, return the palindrome closest to that integer, leaving out n itself. Closeness is the absolute difference between the two integers. If two palindromes are equally close, return the smaller one.

Rules

  • A palindrome is a non-negative integer whose decimal digits read the same forwards and backwards. Every single-digit number, including 0, is a palindrome.
  • The answer must differ from the integer n, even when n is itself a palindrome.
  • Return the answer as a decimal string with no leading zeros, for example "121" or "0".

The integers involved can exceed 2^31 - 1 and also 2^53: n can be as large as 10^18 - 1, and the answer can be as large as 10^18 + 1 (19 digits). Every value fits in a signed 64-bit integer (long in Java, long long in C++); JavaScript needs BigInt for exact arithmetic.

Example 1

Input: n = "123"
Output: "121"

121 is 2 away from 123. The next palindrome above, 131, is 8 away.

Example 2

Input: n = "100"
Output: "99"

99 and 101 are both 1 away from 100, so the smaller one, 99, is returned.

Constraints

  • 1 <= len(n) <= 18
  • n consists only of digits and has no leading zeros.
  • n represents an integer from 1 to 10^18 - 1.

Examples

Example 1
Input
('1',)
Output
'0'
Notes
Source example: 1 itself is excluded, 0 and 2 tie at distance 1, and the smaller 0 wins.
Example 2
Input
('9',)
Output
'8'
Notes
Largest single digit: 8 is 1 away, 10 is not a palindrome, and 11 is 2 away.

Constraints

  • •1 <= len(n) <= 18
  • •n consists only of digits and has no leading zeros.
  • •n represents an integer from 1 to 10^18 - 1.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def nearest_palindrome(n) -> str:
Sample call
nearest_palindrome('1')
Language
Case 1
Input
('1',)
Expected
'0'
Case 2
Input
('9',)
Expected
'8'

Browse More Questions

Show the approach

Approach

Let L be the number of digits of n and h = ceil(L/2). A palindrome with L digits is fixed by its first h digits (its prefix), and a larger prefix always gives a larger palindrome. Let P be the integer formed by the first h digits of n. Every L-digit palindrome whose prefix exceeds P is greater than n, and the smallest of them mirrors P + 1. Every L-digit palindrome whose prefix is below P is less than n, and the largest of them mirrors P - 1. The mirror of P itself can fall on either side of n or equal it. Palindromes with fewer digits are at most 10^(L-1) - 1 (all nines, or 0 when L = 1), and palindromes with more digits are at least 10^L + 1. So the closest palindrome on each side of n is among five candidates: 10^(L-1) - 1, 10^L + 1, and the mirrors of P - 1, P and P + 1. Mirroring appends the prefix reversed and, for odd L, does not repeat the middle digit. When P + 1 gains a digit (P is all nines), its mirror is skipped, because 10^L + 1 is smaller and still above n; this also keeps every candidate inside signed 64-bit range. When P - 1 loses a digit (P is a power of ten), no L-digit palindrome has a smaller prefix, its mirror is a shorter palindrome, and 10^(L-1) - 1 already covers that side. The candidate equal to n is dropped, and the remaining candidate with the smallest absolute difference wins, with ties going to the smaller value. 10^(L-1) - 1 is always below n, so at least one candidate survives. Edge cases: n = "1" returns "0" through the tie rule, 10 and 11 return 9, all-nines inputs return 10^L + 1 (19 digits for the upper bound 10^18 - 1), powers of ten return the all-nines value one digit shorter through the tie rule, and 18-digit inputs need exact 64-bit arithmetic (BigInt in JavaScript), since a double rounds 500000000000000005 and 499999999999999994 to the same value.

Time complexity:
O(L)
Space complexity:
O(L)