Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Closest Palindrome
00:00
5 left

Closest Palindrome

HardPython

Problem

The Uber Drivers app occasionally formats numeric values such as driver-facing trip references in a way that benefits from a nearby palindromic value. Given a positive integer as a decimal string, return the closest different positive-or-zero palindrome.

If two palindromes are equally close, return the smaller one.

Formal Specification

Implement closest_palindrome(n), where n is a decimal string with no leading zeros unless n is "0". Return a decimal string representing the palindrome p that minimizes abs(int(n) - int(p)), subject to p != n.

The result may be "0". You may use integer arithmetic within the stated constraints.

Constraints

  • 1 <= len(n) <= 18
  • n contains only decimal digits
  • n has no leading zeros unless it is "0"
  • The input represents a non-negative integer
  • The answer must be different from the input

Function Signature

def closest_palindrome(n):
Interviewer

Your question is Closest Palindrome. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.