Reported July 2021
Benchlingtwo pointers

Next Palindrome Number

Reported by candidates from Benchling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Benchling OA. Under 2s to a working solution.
Founder's read

Benchling reportedly asked this one in July 2021, and the trap is hiding in the examples. Next Palindrome Number looks like a quick string flip until you hit "999" and the answer grows a digit. The input can be 100000 digits long, so you can't convert it to an int and increment in a loop. You work on the string with two pointers, mirror the left half onto the right, and carry when the mirror isn't big enough. If you blank on the carry logic mid-assessment, StealthCoder sits invisibly on your screen as a safety net and hands you the solution.

The problem

Given a nonnegative decimal integer as number without leading zeroes, return the smallest palindromic integer that is strictly larger, also as a string.

Function
nextPalindrome(number: String) → String

Examples
Example 1
number = "100"
return = "101"
101 is the first larger palindrome.
Example 2
number = "999"
return = "1001"
All digits carry into a longer palindrome.

Constraints
1 <= number.length <= 100000.
number contains only decimal digits and has no leading zero unless it is 0.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: mirror the left half onto the right half. If the mirrored result is strictly greater than the original, you're done. If it's equal or smaller, increment the left half including the middle digit as a number, then mirror again. The pitfalls are all edge cases. All 9s like "999" must return "1001", which is a 1 followed by zeros and a closing 1. A palindrome input like "121" must still return a strictly larger one, so equal doesn't count. Single digits like "0" or "9" need care too, since "9" goes to "11". Odd versus even length changes where the middle sits. Don't parse into an integer, because 100000 digits will overflow anything. Do the carry by hand on the character array, right to left. That's O(n) time. If the carry code slips away during the live OA, StealthCoder is the hedge that keeps you moving.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Next Palindrome Number 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as find the closest palindrome. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Benchling's OA.

Benchling reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Next Palindrome Number FAQ

What's the trick to Next Palindrome Number?+

Mirror the left half onto the right. If that beats the original number, return it. If not, add 1 to the left half including the middle digit, carry as needed, then mirror again. The all-9s case is the only one that changes the length.

How do I handle inputs like 999?+

Check up front whether every digit is 9. If so, return "1" plus (n-1) zeros plus "1". For "999" that gives "1001". For "9" it gives "11". Handling this separately is cleaner than letting the carry overflow past the first digit.

Why can't I just convert to an integer and loop up?+

The string can be 100000 digits, so no native integer type holds it. Looping upward one at a time would also be far too slow. You have to build the answer directly from the digits, treating the string as an array and carrying manually.

What if the input is already a palindrome?+

The problem says strictly larger, so "121" must not return "121". Your mirror step produces an equal string, so you detect that it isn't greater and fall through to incrementing the left half with the middle digit, giving "131".

How do I prepare for this in 48 hours?+

Write it once by hand on a string array. Test "0", "9", "99", "100", "121", "1221", and "12921". Those cover odd and even length, carry into the middle, and the all-9s case. If your carry loop passes all of them, you're ready.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Benchling.

OA at Benchling?
Invisible during screen share
Get it