Amazon Interview Question for Software Engineer / Developers






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

int Palindrome(node *p){
     static node *head=p;
     static int result=2;
     if(p){
            result=Palindrome(p->next);
            if(result==0||result==1) return result;
            if(head->data==p->data){
                      if(head==p||head->next==p) return 1;
                      head=head->next;
             }else return 0;
     }
  return result;
}

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

hey this solution is awesome and compact. nice work there

- slimboy June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

i think u both are drunk!!

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

Really its excellent solution...

- javed.alam June 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

excellent solution.....i dont how u guys get these ideas but u guys are awesome ...:)!!!!....do u guys have work experience or what......totally impressed by this site...
....really intelligent people....

- Anonymous July 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Nice algo, but needs little modification:
int Palindrome(node *p, bool bInternalCall = false)
{
static node *head;
static int result;
if(!bInternalCall)
{
head = p;
result = 2;
}

if(p){
result=Palindrome(p->next, true);
if(result==0||result==1) return result;
if(head->data==p->data){
if(head==p||head->next==p) return 1;
head=head->next;
}else return 0;
}
return result;
}

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

WAP stands for what ?

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

wap = write a program?

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

<pre lang="" line="1" title="CodeMonkey45706" class="run-this">bool check(list<char>& lst,list<char>::iterator traverse, list<char>::iterator itr,list<char>::iterator& itr1);
int main()
{
//Check whether linked list is a palindrome
list<char> lst;
cout<<"Enter the number of elements ";
int num;
cin>>num;
cout<<"Enter the characters ";
for(int i=0;i<num;++i)
{
char val;
cin>>val;
lst.push_back(val);
}
list<char>::iterator itr=lst.begin();
if(num == 1)
{
cout<<"It is a palindrome\n";
exit(0);
}
for(int i=0;i<num/2-1;++itr,++i);
list<char>::iterator itr1=itr;
++itr1;
if(num%2)
++itr1;
if(check(lst,lst.begin(),itr,itr1))
cout<<"Palindrome\n";
else
cout<<"Not a palindrome";
}

bool check(list<char>& lst,list<char>::iterator traverse, list<char>::iterator itr,list<char>::iterator& itr1)
{
if(traverse != itr)
{
list<char>::iterator tempItr = traverse;
if(!check(lst,++tempItr,itr,itr1))
return false;
}
if(*traverse != *itr1)
return false;
++itr1;
return true;
}</pre><pre title="CodeMonkey45706" input="yes">
</pre>

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

Hope it is double-link list, otherwise.

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

why would Amazon ask that simple question?

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

<pre lang="" line="1" title="CodeMonkey93390" class="run-this">public boolean isPalindrome(Node head) {
if (head.next == null) {
return true;
}

Node prev = null, current = head;
while (current.next != null) {
prev = current;
current = current.next;
}

if (head.value != current.value)
return false;

if (head.next == current)
return true;

prev.next = null;
return isPalindrome(head.next);
}</pre><pre title="CodeMonkey93390" input="yes">
</pre>

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

solutions looks elegant. but you are destroying the original list right ?

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

you are destroying the original linked list and leaking all the memory it holds

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

Find the middle element in the list. Reverse all elements after the middle element

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

Please check my code:

int ispalindromeEFF(node* head)
{
	if(head==NULL)
	{
		return 0;
	}
	node *xq,*y,*p,*r;
	int flag=1;
	p=NULL;
	xq=head;
	r=xq->link;
	y=head;
	
	while(y->link && y->link->link)
	{
		y=y->link->link;
		//reverse
		xq->link=p;
		p=xq;
		xq=r;
		if(r)
		{
			r=r->link;
		}
	}
	if(y->link) //even length
	{
		xq->link=p;
		p=xq;
		xq=r;
		r=r->link;
		y=xq;
	}
	else
	{
		y=r;
	}
	node *temp;
	temp=p;
	p=xq;
	xq=temp;
	if(xq)
	{
		r=xq->link;
	}
	while(y)
	{
		if(y->data != xq->data)
		{
			flag=0;
		}
		y=y->link;
		xq->link=p;
		p=xq;
		xq=r;
		if(r)
		{
			r=r->link;
		}
	}
	return flag;
}

Feedbacks are welcome

- ananthakrishnan.s.r June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

LinkedList* LinkedList::IsPalindrom(LinkedList *cur, LinkedList *head)
{
if(cur == NULL)
return NULL;
LinkedList *temp = NULL;
if(cur->next != NULL)
temp = IsPalindrom(cur->next, head);
else
temp = head;
if(temp == NULL)
return NULL;

if(cur->data == temp->data)
if(cur == head)
return temp;
else
return temp->next;
else
return NULL;
}

Call Like this

if(list->IsPalindrom(list, list) == NULL)
{
cout << "Not Palindrom " << endl;
}
else
cout << "Yes Palindrom " << endl;


You cant get better solution than this :)

- Ankit is genius June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is mean by Palindrome?

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

node *
traverse_to_list_mid(node *n);

int
palindrome (node  **first, node *last)
{
    if (last == NULL)
        return 1;

    ret = palindrome (first, last->next);
    if (ret == 0)
        return 0;

    if ((*first)->data == last->data) {
        (*first) = (*first)->next;
        return 1;
    } else {
        return 0;
    }
    return 0;
}

int
main ()
{
    node *first, *last;
    last = traverse_to_list_mid (first);
    ret = palindrome (&first, last);
    if (ret == 0)
        printf ("not a palindrome");
    else 
        printf ("Palindrome");
}

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

Add all the elements to a stack...
On the next traversal, compare elements in the stack with the elements in the list...

Time Complexity = Space Complexity = O(n)

- Rahul Arakeri January 28, 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