Amazon Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
0
of 0 vote

I would traverse the list with slow and fast pointers , with slow pointer visiting every node and fast pointer visiting alternative node.The slow pointer data is stored in a stack until the fast pointer(odd case) or its next reaches null(even case).Now compare the slow poniter data with the element popped from stack.If all the comparisons are true until the stack is empty, then it is a palindrome.

TC- Order of N
SC- O(N)

We can also think of an alternative solution without using the extra space.

- krantboy June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice n elegant

- If_else June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

use a third pointer, which will start from head of list once the slow pointer reaches mid

- ps June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome Dear. Send me your email id
2 goelnavneet2006@gmail.com

- Navneet July 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

goelnavneet2006@gmail.com

- Navneet July 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
	 * Time: O(N)
	 * Space: O(1)
	 */
	public static boolean isListPalindrome(List<Integer> list){
		
		if( list == null ){
			throw new IllegalArgumentException("NULL list can't be checked for palindrome");
		}
		
		if( list.isEmpty() ){
			return true;
		}
		
		final int halfSize = list.size()/2;	
		
		Iterator<Integer> headIt = list.iterator();			
		BigInteger sum1 = sumOfNumbers( headIt, halfSize );
		
		Iterator<Integer> middleIt = getMiddlePointer(list);
		BigInteger sum2 = sumOfNumbers( middleIt, halfSize );		
			
		return sum1.equals(sum2);		
	}

- m@}{ June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Such a fool you are !!
Idiot .. :P

- If_else June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sraniy indus, past zakroy.

- m@}{ June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think ur logic ll not work for list like this 1 2 3 1 2 3.according to ur logic it ll return as a palindrome.but it s not a palindrome right?

- hari June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think ur logic ll not work for list like this 1 2 3 1 2 3.according to ur logic it ll return as a palindrome.but it s not a palindrome right?

- hari June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Hari. according to above logic 123 123 is not a palindrome. and the logic is correct,

- Anonymous June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we can modify the original list itself then I think we can solve it without using extra space.

1. Just reach the middle element of the the given linked list using slow and fast pointers and from the mid point reverse the second half of the original list.
2. Do a traversal one pointer at start and second at the middle element and compare node by node basis
Overall takes O(n).

Please let me know how you find this.

- SS June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sounds effective

- Old Monk June 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But I would prefer extra space over modifying the list.

- krantboy June 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

someone posted this logic for the same question posted earlier. Hope this helps. I thought it's a very elegant way to solve this problem.

bool LinkedList::isPalindrome(){
	if (NULL == head)
	{
		cout<<"List is empty, can't check palindrome property"<<endl;
		return true;
	}
	LinkedListNode *endNode = head;
	LinkedListNode *tempHead = head;
	LinkedListNode **firstNode = &tempHead;
	bool retval = checkPalindrome(firstNode,endNode);
	if (retval)
		cout<<"The List is a palindrome"<<endl;
	else
		cout<<"The List is not a palindrome"<<endl;

	return retval;
}

bool LinkedList::checkPalindrome(LinkedListNode **firstNode, LinkedListNode *endNode){

	//base case
	bool retval = true;
	if (NULL == endNode)
		return retval;

	retval &= checkPalindrome (firstNode, endNode->getNext());
	if (!retval){}
	else if ((*firstNode)->getKey() == endNode->getKey())
		retval = true;
	else
		retval = false;

	*firstNode = (*firstNode)->getNext();

	return retval;
}

- BlackJack June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry for incorrect solution.

/**
	 * Time: O(N)
	 * Space: O(N/2)
	 */
	public static boolean isListPalindrome(List<Integer> list){
		
		if( list == null ){
			throw new IllegalArgumentException("NULL list can't be checked for palindrome");
		}
		
		if( list.isEmpty() ){
			return true;
		}
		
		List<Integer> firstHalf = getFirstHalf(list);
		Collections.reverse(firstHalf);
		
		Iterator<Integer> middleIt = getMiddlePointer(list);
		Iterator<Integer> firstHalfReversedIt = firstHalf.iterator();
		
		while( middleIt.hasNext() && firstHalfReversedIt.hasNext() ){
			int value1 = firstHalfReversedIt.next();
			int value2 = middleIt.next();
			
			if( value1 != value2 ){
				return false;
			}
		}
		
		return !(middleIt.hasNext() || firstHalfReversedIt.hasNext());
	}

- m@}{ June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I doubt if anyone ever saw in a text an expression like O(n/2) space complexity. Learn first, then post solution here!

- anon June 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is one of the ways using XOR.

class Node
{
public Node Next = null;
public int data;
public int count=0;
public Node(int d) { data = d; count++; }

public void AddNewNode(int input)
{
Node newnode = new Node(input);

Node n = this;

while (n.Next != null)
{
n = n.Next;
}
n.Next = newnode;

count++;
}
}

public static bool isLinkedListIsPalindrome(Node myLL)
{
int xorvalue=0,TotalNodes=myLL.count,iteratorval=0,midvalue=0;;
bool ispalindrome = false;
while (iteratorval<TotalNodes)
{
xorvalue = xorvalue^myLL.data;
myLL = myLL.Next;
if(iteratorval==(TotalNodes/2))
{
midvalue = myLL.data;
}
iteratorval++;
}

if ((0 == TotalNodes % 2))
{
if(0==xorvalue)
ispalindrome = true;
}
else
{
if (midvalue == xorvalue)
ispalindrome = true;
}
return ispalindrome;
}

- Ram: June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Respect!! \m/

- Abby September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok I take that back.. false positive 11322

- Abby September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)We can reverse the linked list until the middle element.(We can find the middle element using slow and fast pointers)
2)Then traverse to the ends of linked list checking the validity of palindrome.
3)Reverse the linked list again until the middle element.
time complexity - O(n),space complexity - 0(1)

- Satish June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can just reverse the linked list and check if it is same as the original one.

- Saahithi July 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Reverse is easy, but how do you compare it with original list?

- Anonymous September 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys, we can do it without reversing the linked list.

int isPalindrome(**root,*head)
{
	if(!head)
		return 1;
	else
	{
		int res=isPalindrome(root,head->next);
		
		if(res==0)
			return 0;
		int r=(*root)->data==head->data;
		*root=(*root)->next;
		return r;
	}
}

- Aashish June 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPalindrome(node *head)
{
if(head==NULL || head->next==NULL)return true;
  
node *prev=NULL, *curr=head;
while(curr->next)
{
prev=curr;
curr=curr->n;
}

//Now curr points to last node
if(head->data !=curr->data) return false;
  
//Remove the last node from the list
prev->next=NULL;

//Recursively check for next item from front
return isPalindrome(head->next);
}

- nagarajmailing August 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it is easier than you think. We want to start with the first element in the list and the last element in the list. Then we will compare the values and make sure they are the same as we slowly move towards the inside of the list. If the values do not match at any time, then it is not a palindrome. If the starting variable become > our last variable than we can return.


public boolean checkForPal(List list) {
left = 0;
right = list.size()-1;

while(left <= right) {
if(list.get(left) != list.get(right)) {
return false;
}
left++;
right--;
}
return true;
}

- ShaunW August 29, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More