Check whether a Python string reads the same backwards using two pointers in O(n) time and O(1) space, then compare with the slicing one-liner.
The problem
Return True if s reads the same forwards and backwards. Compare characters exactly (case-sensitive), and use two pointers rather than s[::-1].
Examples
Example 1
Input
is_palindrome('madam')Expected output
True
Example 2
Input
is_palindrome('python')Expected output
False
+ 4 hidden tests on Submit — even length, case-sensitive.
Edge cases to ask about
- Empty string
- Single character
- Even vs odd length
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Reverse and compare | O(n) | O(n) | s[::-1] builds a full reversed copy. |
| bestTwo pointers | O(n) | O(1) | Compare from both ends inward; stop at the first mismatch. |
Walkthrough of the optimal approach (try it yourself first)
Put i at the start and j at the end, compare, and walk them toward each other. The first mismatch means False; meeting in the middle means True.
s == s[::-1] is also O(n) time, but it allocates a full reversed copy (O(n) space) and cannot stop early.
Complexity: O(n) time, O(1) space. At most n/2 comparisons and two integer indices — no copy of the string.
Reveal the reference solution
def is_palindrome(s): i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
The brute force, for comparison
def is_palindrome(s): return s == s[::-1]
Follow-ups interviewers ask
- Ignore non-alphanumeric characters and case ('A man, a plan…').
- Valid if you may delete at most one character.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Check If a String Is a Palindrome in Python?
The optimal solution runs in O(n) time and O(1) auxiliary space. At most n/2 comparisons and two integer indices — no copy of the string.
What is the brute-force approach, and how do you optimise it?
Reverse and compare: O(n) time, O(n) space. s[::-1] builds a full reversed copy. Two pointers: O(n) time, O(1) space. Compare from both ends inward; stop at the first mismatch.
What follow-up questions do interviewers ask about Check If a String Is a Palindrome?
Ignore non-alphanumeric characters and case ('A man, a plan…'). Valid if you may delete at most one character.
