Seventh Nearest Palindrome
Reported by candidates from Deloitte's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Deloitte reportedly sent this one in May 2026, and the constraint is the whole joke. n goes up to 10^18, so walking outward one number at a time until you've found seven palindromes is dead on arrival. The hinted pattern is two-pointers, with one pointer expanding down and one expanding up from n. You only ever need a handful of candidates, and you build them from the left half of the number instead of scanning. If you've got an invite and 48 hours, learn that construction. StealthCoder sits invisible on your screen as a safety net if you blank during the live OA.
The problem
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 = 100 return = 131 The nearest palindromes in order are 101, 99, 111, 88, 121, 77, 131. Therefore the 7th nearest palindrome is 131. Example 2 n = 10 return = 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: palindromes are fully determined by their first half. Take the prefix of n (the first ceil(len/2) digits), then generate palindromes from prefix-k through prefix+k for a small k, mirroring each one. Do this for the current length, and also add edge cases like 99...9 and 10...01 for the lengths one shorter and one longer. Collect them into a set, drop anything under 10, then sort by distance with ties going to the larger value, and pick the 7th. Pitfalls: forgetting that n itself counts when it's a palindrome, forgetting the tie rule, missing lengths that change when the prefix rolls over (like 99 to 100), and overflowing past 64-bit in other languages. Seven results means k around 10 is plenty. If the construction slips under pressure, StealthCoder is the hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Seventh Nearest Palindrome cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Deloitte's OA.
Deloitte reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Seventh Nearest Palindrome FAQ
What's the trick to Seventh Nearest Palindrome?+
Build palindromes from the left half instead of scanning numbers. Take the prefix of n, try prefix values a few steps above and below, mirror each one, and you get candidate palindromes in constant work. Then sort by distance and pick the 7th.
Why can't I just loop outward from n?+
With n up to 10^18, palindromes can be astronomically far apart in the worst case, and the statement explicitly warns against scanning one number at a time. Palindromes near a big n are spaced roughly 10^9 apart, so a linear walk never finishes.
How do I handle the tie-breaking rule?+
Sort candidates by the pair (|p - n|, -p). That puts the larger palindrome first when two are equally far from n. Example 1 shows it: 101 comes before 99 for n = 100, since both sit at distance 1 and the greater one wins.
What edge cases break most solutions?+
Length changes. Near n = 10 or n = 100, valid palindromes include shorter ones like 99 or 9-digit-to-10-digit rollovers like 10...01. Also single digits must be excluded, so for n = 10 the answer is 77, not something with 7 itself.
How do I prep for this in 48 hours?+
Write the candidate generator once: extract prefix, vary it by a small offset, mirror for both odd and even lengths, add the 99...9 and 10...01 boundary values. Then test it against Examples 1 and 2 plus a few n values near powers of ten.