Sample interview problems

Palindrome Linked List

This problem asks whether a given singly linked list is a palindrome. A palindrome is a word, phrase, or sequence that reads the same backward as forward. In this case, the list is a palindrome if the elements in the list read the same forward as backward.


Given a singly linked list, write a function to check if the list is a palindrome. Return true if it is, false otherwise.
Input: 1->2->3->2->1
Output: true


This solution is in Python. Our A.I. can create solutions in multiple languages.
by AskAI
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
# = next
class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        if not head:
            return True
        slow = head
        fast = head
        while fast and
            slow =
            fast =
        if fast:
            slow =
        slow = self.reverse(slow)
        while slow:
            if slow.val != head.val:
                return False
            slow =
            head =
        return True
    def reverse(self, head):
        prev = None
        while head:
            temp =
   = prev
            prev = head
            head = temp
        return prev

A.I. Evaluation of the Solution

The candidate's solution correctly checks if a singly linked list is a palindrome. The approach is to use a slow and fast pointer to find the middle of the list, reverse the second half of the list, and then compare the two halves. One potential improvement to the solution would be to use a stack to keep track of the first half of the list instead of reversing the second half. This would avoid having to reverse the second half of the list, which is unnecessary work.

Evaluated at: 2022-10-23 23:29:24