Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Palindrome With One Removal

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Palindrome With One Removal. Start with the requirements on the right.

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.

Problem

Instagram uses lightweight text-processing checks in several caption and messaging workflows. Given a string, determine whether it is already a palindrome or can become one after removing at most one character.

A palindrome reads identically from left to right and right to left. Comparisons are case-sensitive, and every character, including spaces and punctuation, must be considered.

Formal Specification

Implement valid_palindrome(s):

  • Input: s, a string containing printable ASCII characters.
  • Output: Return True if s is a palindrome after removing zero or one character. Otherwise, return False.

Your solution should use O(1) auxiliary space and avoid trying every possible deletion when a mismatch can identify the only relevant candidates.

Constraints

  • 1 <= len(s) <= 100,000
  • s contains printable ASCII characters
  • Comparisons are case-sensitive
  • At most one character may be removed

Function Signature

def valid_palindrome(s):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output