Amazon Interview Question for Software Engineer / Developers


Team: Development
Country: India
Interview Type: In-Person




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

Use the concept of slow and fast pointers
With one small change while traversing through the list with the slow pointer load
the values onto a stack
Now after you reach the middle from step 1 traverse the
remaining list with the slow pointer and pop each value from the stack
At each point these values should match if not return false or at the end return true

- Hello world February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is my code

public boolean isPalindrome(LinkedListNode head)
	{
		if(head == null)
			return false;
		Stack<Integer> stack = new Stack<Integer>();
		LinkedListNode slow = head;
		LinkedListNode fast = head;
		stack.add(slow.value);
		int i = 0;
		while(fast.next != null)
		{
			if(i == 0)
			{
				fast = fast.next;
				i = 1;
			}
			else if(i == 1)
			{
				slow = slow.next;
				fast = fast.next;
				stack.add(slow.value);
				i = 0;
			}
		}		
		System.out.println(slow.value);
		if(stack.size() % 2 == 0)
		{
			stack.pop();
		}
		slow = slow.next;
		while(!stack.isEmpty() && slow != null)
		{
			int val = stack.pop();
			if(slow == null ||  val != slow.value)
				return false;
			slow = slow.next;			
		}
		return true;
	}

- hello world February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the algorithm is great, I put more compact java code below:

public static boolean isPalindrome(LinkedList<Character> input) {
		if (input == null || input.size() == 0)
			return false;
		
		Iterator<Character> fast = input.iterator();
		Iterator<Character> slow = input.iterator();
		
		Stack<Character> firstHalf = new Stack<Character>();
		int i =0;
		while(fast.hasNext()){
			fast.next();
			if((i & 1) == 0)
				firstHalf.push(slow.next());
			i++;
		}
		if ((input.size() & 1) != 0){
			firstHalf.pop();
		}
		while(slow.hasNext()){
			if(!firstHalf.pop().equals(slow.next())){
				return false;
			}
		}
		return true;
	}

- vinc March 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about this implementation in place.
We reverse the sencond half of the list and compare half of the list.
I think It more efficient.
IF WE have to leave the linked list as Its initial state, then we can just reverse the half again.

public static boolean  isPalindrome(LNode node){
		int size = size(node);
		int middle = size/2;
		
		if((size % 2) != 0) middle++; // special clase for odd linked list size
		
		LNode current = node;
		LNode runner = node;
		int counter = 0;
		while(counter < middle-1){
			runner = runner.next;
			counter++;
		}
		runner.next = reverse(runner.next);
		runner = runner.next;
		while(runner != null){
			if(runner.value != current.value){
				return false;
			}
			runner = runner.next;
			current = current.next;	
		}
		
		return true;
		
	}

	public static LNode reverse(LNode node){
		LNode current = node;
		LNode next = null;
		LNode prev = null;
		
		while(current != null){
			next = current.next;
			current.next = prev;
			prev = current;
			current = next;
		}
		return prev;
	}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> February 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If modification is allowed the reverse the half list accross to it's center and and check for the palindrome....

Other wise hashing will do if modification is not allowed

- NaiveCoder February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If modification is allowed the reverse the half list accross to it's center and and check for the palindrome....

Other wise hashing will do if modification is not allowed

- NaiveCoder February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use slow/fast pointers to reach the middle node. While doing this reverse the first half of the linked list inplace. Now you have two linked lists -> compare node by node. While doing this reverse the first half again.

p = q = head;
prev = NULL;	
while( q != NULL )
{
	q = q->next;
	if( q->next != NULL )
		q = q->next;
		break;
	
	// reverse the linked list till p - middle node
	temp = p->next;
	p->next = prev;
	prev = p;
	p = temp;
}

forward = temp;
// move p one step backward for linked lists with odd number of elements
if( q == NULL )
	temp = p->next;
	p->next = forward;
	p = temp;
	prev = p;
else
	prev = temp;
backward = p;
	
bool palindrome = true;
while( backward != NULL )
{
	if( forward->data != backward->data )
		palindrome = false; //don't return yet, you have to reverse the list fully
		
	
	// reverse first half of the linked list again
	forward = forward->next;
	temp = backward;
	backward->next = prev;
	backward = temp;
}
return palindrome;

- y2km11 February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

This comment has been deleted.

- Administrator February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wow.. so, i am told 345264 is a palindrome.. Great Pankaj!!

- Great!! February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't we do like this?

Traverse the linked list, read the characters and append that to a string. Take the substring from 0 to N/2-1 and from N/2 to N-1. Reverse the second substring, compare them return the result. If N is odd, take the substrings from 0 to N/2-1 and N/2+1 to N-1.

- Vijay March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool chkPalindrom(Node root,Queue queue){

if(root==null)
return true;
queue.add(root.data)
if(!chkPalindrom(root.next,queue))
return false;
if(queue.pop()==root.data)
return true;

}

- sundi133 March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

maintaining a seprate queue and compare the front element with the elements on the the recursion stack.

- sundi133 March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this would take as much time as reversing. Is this too simple of an answer?

public static boolean isPalindrome( LinkedList list )
{
	int n = list.size();
	boolean state = false;
	for (int i = 1; i <= n; i++, n--)
	{
		if ( list.get(i) == list.get(n) )
			state = true;
		else
		{
			state = false;
			break;
		}
			
	}
	return state;
}

- t-rev March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution is simple coz it uses built-in get() function.
If the list is a singly linked list, then get() function is O(n).
Doing it this way would result in a complexity of O(n^2)

- bhosale.praful March 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple Solution in O(n) using recursion

private static boolean isPalindrome(Node<Character> startNode,
			StringBuffer fwdStr, StringBuffer revStr) {

		fwdStr.append(startNode.data);
		if (startNode.next != null) {
			isPalindrome(startNode.next, fwdStr, revStr);
		}
		revStr.append(startNode.data);
		return fwdStr.toString().equals(revStr.toString());

	}

- Guru March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ int isPalin(node **head, node *cur) { if(cur==null) return 1; if(isPalin(head, cur->next) && *head->data==cur->data) { *head=*head->next; return 1; } return 0; } - Rahul May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

We can simply use an equation...

ax^n + b x^n-1 + C x^n-2 +..............+ Z X^0

a, b, c represents ascii value for each char in the linked list..
Take any arbitrary value for X,
N represents LinkedList lenght -1

Traverse the list from left to right , and right to left and if the sum is same in both the cases.
They are palindrome.

- Vijay Rajanna February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will you traverse a singly list from both sides right to left and left to right?

- Ajay Kumar February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just reverse d list..

- vijay sringeri February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just reverse d list..

- vijay sringeri February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just reverse d list..

- vijay sringeri February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just reverse d list..

- vijay sringeri February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just reverse d list..

- vijay sringeri February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no need of reversing the list.
first calculate a[0]*x^0+a[1]*x^1......+a[n-1]*x^(n-1).
where a[i] means ascii value of character in the list present at position i.
from this you will know n.
now calculate a[0]*x^(n-1)+a[1]*x^(n-2)+......a[n-2]*x^1+a[n-1]*a^0
if both values are same then given list is pallindrome.

- Anonymous June 14, 2013 | Flag


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