Amazon Interview Question for Software Engineer / Developers


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




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

geeksforgeeks.org/archives/1072

- pk January 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The following algorithm will have O(n) time and O(n) space complexity...

1) Start from the first node and add every element to a stack.
2) Once the stack is created, again start from the beginning of the LL and compare top of stack with the element in the LL.
3) The moment you find a mismatch, exit the loop and report that it is not a palindrome. If the stack gets emptied, it indeed is a palindrome.

Having given this algorithm, I would like to know if there is a better approach than O(n). Thanks :)

- Rahul Arakeri January 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

count number of element in the list, O(n).
break list at n/2 and invert the second part of the list. O(n)
compare node by node to verify.
Once cone, invert the second list and link its head to the tail for first list.
Overall complexity O(n), space complexity const.

- abhishekatuw January 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I will put all the elements in the LL in a String. After that I will check if that String is palindrome by either :
a) reversing the string and comparing
b) checking it myself for example:

public static boolean isPalindrome(char[] data, int start, int end) {
		int len = (end - start) + 1;
		for (int cursor = start; cursor < start + (len / 2); cursor++) {
			if (data[cursor] != data[end - cursor + start])
				return false;
		}
		return true;
	}

- arsh January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ispalin(node** head,node* last)
{
	if(last->next==NULL) return true;
	cout<<"!!! comparing "<<(*head)->ch<<" "<<last->ch<<" "<<last->next<<" "<<(*head)->next<<" "<<head<<" "<<last<<endl;
	bool temp = (ispalin(head,last->next) && ((*head)->ch == last->ch));
	cout<<"comparing "<<(*head)->ch<<" "<<last->ch<<" "<<last->next<<" "<<(*head)->next<<" "<<head<<" "<<last<<endl;
	(*head)=(*head)->next;
	return temp;
}
cout<<ispalin(&head,temp)<<endl;;

- Pratyush January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ispalin(node** head,node* last)
{
	if(last->next==NULL) return true;
	bool temp = (ispalin(head,last->next) && ((*head)->ch == last->ch));
	(*head)=(*head)->next;
	return temp;
}
cout<<ispalin(&head,temp)<<endl;

- Pratyush January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)Take two pointers
2) Traverse the first pointer to the end of the LL
3) Now increment the second pointer by 1 and decrement the first pointer by 1.
4) Compare their values in the position.
5) If there is a mismatch before the pointers intercept then its not a palindrome.

so you require n+n/2 passes .. hence the complxity would be O(n)

- Praveen L June 21, 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