FastPrepSeventh Nearest Palindrome
Problems/Seventh Nearest Palindrome

Seventh Nearest Palindrome

Deloitte logoDeloitteINTERNOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an integer n. Return the 7th nearest palindrome to n.

A valid palindrome for this problem must have at least two digits, so single-digit numbers from 1 to 9 are not considered.

Consider every valid palindrome p, including palindromes less than n, equal to n, and greater than n. Sort those palindromes by increasing distance |p - n|. If two palindromes have the same distance from n, place the greater palindrome first. Return the 7th palindrome in this order.

Function

seventhNearestPalindrome(n: long) → long

Examples

Example 1

n = 100return = 131

The nearest palindromes in order are 101, 99, 111, 88, 121, 77, 131. Therefore the 7th nearest palindrome is 131.

Example 2

n = 10return = 77

Single-digit palindromes are ignored. The nearest valid palindromes are 11, 22, 33, 44, 55, 66, 77.

Constraints

  • 1 ≤ n ≤ 1018
  • The input type is long.
  • Single-digit numbers (1–9) are not considered valid palindromes.
  • The solution should avoid scanning outward one number at a time, as the function may be called for many test cases.

More Deloitte Online Assessment questions

  • Minimum Deletions for a Non-PalindromeOA · Seen Jul 2025
  • Minimum-Cost Solar Rooftop AllocationOA · Seen Jul 2025
  • Pair Elements with Even SumsOA · Seen Jul 2025
  • Count Divisible PermutationsOA · Seen Jan 2025
  • Largest Element in an ArrayOA · Seen Nov 2024
  • Install Carbon FiltersOA · Seen Oct 2023
  • Longest Substring with Even OccurrencesOA · Seen Oct 2023
  • Maximum Sum RectangleOA · Seen Apr 2022
  • Annual Bank Balance with Monthly Card FeesOA · Seen Jan 2022
  • Count Battleships by SizeOA · Seen Jan 2022

Deloitte interview experiences

  • Deloitte New Grad Tech OA and Interview Questions: 14 Experiences
All Deloitte interview questionsSee Deloitte hiring insights
Saved
class Solution {
    public long seventhNearestPalindrome(long n) {
      // write your code here
    }
}
Checking account…