
Tech Community (Students + HR + professionals + Recruiters)
May 27, 2025 at 05:11 PM
Now, let's solve the next Python Coding Interview Question:
*3. Check for Palindrome*
*Problem Statement:*
Determine if a given string is a palindrome (reads the same backward as forward).
*Approach 1: Using String Slicing*
def is_palindrome(s):
s = s.lower().replace(" ", "") # Optional: normalize the string
return s == s[::-1]
# Example usage:
word = "Madam"
print("Is palindrome:", is_palindrome(word))
*Explanation:*
- s[::-1] gives the reversed string.
- Compare the original string with its reverse.
- Used .lower() and .replace(" ", "") to handle case and spaces (optional).
*Approach 2: Using a Loop*
def is_palindrome(s):
s = s.lower().replace(" ", "")
left = 0
right = len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
# Example usage:
word = "RaceCar"
print("Is palindrome:", is_palindrome(word))
*Explanation:*
- Two-pointer technique: compare characters from both ends.
- Efficient for long strings.
Python Coding Interview Questions: https://whatsapp.com/channel/0029Vau5fZECsU9HJFLacm2a/266
Python Coding Challenge: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1661
*React with ❤️ for the next answers*
❤️
1