Dataford
Interview Guides
Upgrade
All questions/Coding/Validate Palindrome in Chat Input

Validate Palindrome in Chat Input

Easy
Coding
Asked at 1 company1StringsTwo PointersMath
Also asked at
OpenAI

Problem

Problem

In an OpenAI chat surface, some validation checks may need to determine whether a user-provided string reads the same forward and backward. Write a function that returns True if a string is a palindrome and False otherwise.

A palindrome is a string that is identical when reversed. For this problem, compare characters exactly as given: do not ignore spaces, punctuation, or letter casing.

Formal Specification

  • Input: a string s
  • Output: a boolean indicating whether s is a palindrome

Examples

Example 1

  • Input: s = "racecar"
  • Output: True
  • Explanation: The string is the same from left to right and right to left.

Example 2

  • Input: s = "openai"
  • Output: False
  • Explanation: Reversing the string produces a different value.

Example 3

  • Input: s = "Aa"
  • Output: False
  • Explanation: Comparison is case-sensitive, so "A" != "a".

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Use an algorithmic solution in Python

A solution with two pointers is expected and should run in linear time.

Examples

Example 1
Inputs = "racecar"OutputTrueWhyEach mirrored pair matches: `r-r`, `a-a`, `c-c`, with `e` in the center.
Example 2
Inputs = "openai"OutputFalseWhyThe first and last characters do not match, so the string cannot be a palindrome.
Example 3
Inputs = "abba"OutputTrueWhyThe outer and inner character pairs are equal, so the string reads the same both ways.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Comparison is exact: do not ignore spaces, punctuation, or case

Function Signature

def is_palindrome(s):

Problem

Problem

In an OpenAI chat surface, some validation checks may need to determine whether a user-provided string reads the same forward and backward. Write a function that returns True if a string is a palindrome and False otherwise.

A palindrome is a string that is identical when reversed. For this problem, compare characters exactly as given: do not ignore spaces, punctuation, or letter casing.

Formal Specification

  • Input: a string s
  • Output: a boolean indicating whether s is a palindrome

Examples

Example 1

  • Input: s = "racecar"
  • Output: True
  • Explanation: The string is the same from left to right and right to left.

Example 2

  • Input: s = "openai"
  • Output: False
  • Explanation: Reversing the string produces a different value.

Example 3

  • Input: s = "Aa"
  • Output: False
  • Explanation: Comparison is case-sensitive, so "A" != "a".

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Use an algorithmic solution in Python

A solution with two pointers is expected and should run in linear time.

Examples

Example 1
Inputs = "racecar"OutputTrueWhyEach mirrored pair matches: `r-r`, `a-a`, `c-c`, with `e` in the center.
Example 2
Inputs = "openai"OutputFalseWhyThe first and last characters do not match, so the string cannot be a palindrome.
Example 3
Inputs = "abba"OutputTrueWhyThe outer and inner character pairs are equal, so the string reads the same both ways.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Comparison is exact: do not ignore spaces, punctuation, or case

Function Signature

def is_palindrome(s):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
&Validate Palindrome in User InputEasyValidate String PalindromeEasyAmazonCheck String Palindrome ValidityEasy
Next question