Amazon Interview Question for Software Engineer in Tests






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

Can any one suggest which one is a better option in this case: adjusting the links to node or just replacing tha values of the alternate nodes.

- Rids August 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Swapping the nodes means swapping the links... that is what the interviewer is looking for... logic to swap the links !

... -> a.prev -> a -> b -> b.next -> ...

keep 3 pointers... a.prev , a, b

(a.prev).next = b
b.next = a
a.next=b.next

- Learner: August 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The sequence is wrong...
(a.prev).next = b
a.next = b.next
b.next = a

- songofsp3 August 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A recursive soln

Node* RecursiveReversePair(Node *head) {
     Node *p = head, *q, *r;
     if( !p || !(p->next)) return p;
     q = p->next->next;
     r = p->next;
     p->next->next = p;
     p->next = RecursiveReversePair(q);
     return r;
}

- Sriram S August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The problem asks to revert a pair of nodes. Given your method signature - which nodes are you swapping ?

- A August 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
struct node
{
struct node *next;
int value;
};

struct node *getnode()
{
return ((struct node *)malloc(sizeof(struct node *)));

}
struct node *head;
int main()
{
int n;
int val,i,flag=0,count=0;
struct node *p,*q,*r,*s,*t;
printf("Enter the number of elements in your linked list\n");
scanf("%d",&n);
printf("Enter the values in your linked list\n");
for(i=0;i<n;i++)
{
scanf("%d",&val);
if(head==NULL)
{
head=getnode();
head->value=val;
head->next=NULL;

}
else
{
p=head;
q=head;
while(p != NULL)
{
q=p;
p=p->next;

}
p=getnode();
p->value=val;
p->next=NULL;
q->next=p;

}


}

p=head;
printf("The elements in the list are \n");
//printing linked list
while(p!=NULL)
{

printf("%d \t",p->value);
p=p->next;

}
printf("\n");
p=head;
q=NULL;
r=NULL;
while(p!=NULL && p->next!=NULL)
{

q=p->next;
printf("Swapping %d and %d\n",p->value,q->value);

p->next=q->next;
q->next=p;

if(r!=NULL)
r->next=q;

if(p==head)
head=q;
r=p;
p=p->next;
}
p=head;
printf("The elements in the list are \n");
//printing linked list
while(p!=NULL)
{

printf("%d \t",p->value);
p=p->next;

}
printf("\n");

}

- Ankur September 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Split list into two list - if 1->2->3->4->5->6->7->8 are the nodes
then two lists will be 1->3->5->7 && 2->4->6->8 now merge both list by making list2 as the first link list... so it will be
2->1->4->3->6->5->8->7

- Anonymous November 29, 2011 | 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