Write code to check if a list is a palindrome.
💡 Model Answer
A palindrome list reads the same forwards and backwards. The simplest approach is to compare the list with its reverse. In Python, you can do:
def is_palindrome(lst):
return lst == lst[::-1]This uses slicing to create a reversed copy of the list and compares it to the original. The time complexity is O(n) because each element is inspected once, and the space complexity is also O(n) due to the reversed copy. If you want to avoid extra space, you can compare elements pairwise from the ends:
def is_palindrome_in_place(lst):
left, right = 0, len(lst) - 1
while left < right:
if lst[left] != lst[right]:
return False
left += 1
right -= 1
return TrueThis version runs in O(n) time and O(1) space. Either implementation is suitable for interview questions about palindrome detection.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500