Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote
{{{ REVERSE(DoubleLinkList head) { DoubleLinkList prev,next,curr; prev = null; curr = head; while(curr != null) { next = curr.next; curr.next = prev; current.parent = next; prev = curr; curr = next; } return prev; - KEEG June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void ReverseDoubleLinklist(Dlinklist p,Dlinklist head)
{
        Dlinklist q;
	if(head==NULL) return;
	if(p->next!=NULL)
	{
		q=p->next;
		p->prev=q;
		ReverseDoubleLinklist(q,head);
	}
	else
	{
		p->prev=NULL;
		return;
	}
	q->next=p;
	if(p==head)
	{
		p->next==NULL;
	}
}

- Charles December 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Basic Idea of reversing a linked list.
1. 3 pointers.(prev , cur, next)
2. In Each iteration,
a) next will be cur`s next
b) prev`s next will be cur
c) cur`s prev will be prev

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

Yes..The correct one..

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

Do we need to reverse the Doubly Linked list? I can simply change the Head to the last node.
As it is double linked, its done I think.
Please correct me if I am missing something.

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

I also thought the same thing, please anyone correct me if I am wrong.

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

But yes.. in the code we have to use prev instead of next.// As per the functionality it doesn't make much sense but it does impacts the readability of the code.

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

reverse(Node** head){

Node* node = *head;
Node* temp = NULL;

while(node != NULL){
temp = node->prev;
node->prev = node->next;
node->next = temp;
node = node->prev;
}

if(temp != NULL)
*head = temp->prev;

}

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

The above code that i wrote swaps the next and prev pointers.

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

reverse node*(struct node *pHead)
{
struct node *r = NULL;
struct node *s = NULL;

while(pHead)
{
s = pHead->next;
pHead->next = r;

r = pHead;
pHead = s;
}
return r;
}

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

#include<iostream>
using namespace std;

class node {
int data;
node *next;
node *prev;
public:
node()
{
data=0;
next=prev=NULL;
}
friend class ll;
};

class ll {
node *start;
public:
ll()
{
start=NULL;
}

void insert()
{
int num;
do {
cout<<"entr num 2 b inserted\n";
cin>>num;
if(num!=-1)
{
node *temp=new node;
temp->data=num;
temp->next=temp->prev=NULL;
if(start==NULL)
{
start=temp;
}
else
{
node *t=start;
while(t->next!=NULL)
t=t->next;
t->next=temp;
temp->prev=t;
temp->next=NULL;
}
}
else ;
}while(num!=-1);
}


void display()
{
node *t=start;
while(t)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<endl;
}

void rev()
{
node *xx=NULL;
node *curr=start;
node *temp=NULL;
while(curr!=NULL)
{
temp=curr->next;
curr->next=xx;
curr->prev=temp;
xx=curr;
curr=temp;
}
start=xx;
}
};

int main()
{
ll l;
l.insert();
l.display();
l.rev();
l.display();
return 0;
}

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

<pre lang="" line="1" title="CodeMonkey76510" class="run-this"> public LLNode Reverse(LLNode node)
{
if (node == null || node.Next == null)
return node;
LLNode head = node, tmpNode = node;
while (tmpNode!=null)
{
tmpNode = head.Next.Next;
head.Next = head;
if (tmpNode != null)
head = tmpNode;
}
node.Next = null;
return head;
}</pre><pre title="CodeMonkey76510" input="yes">
</pre>

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

there are 3 ways :
1. create a new doubly linked list and copy the values with reversing the prev and next pointers
2. swap values of last with first, second-last with second and so on
3. change the head from first to last node and reverse the prev next.
Logically it does not make much difference except the head but when asked for reversing we shud consider the pointers next and prev

- aditya.bokaro August 08, 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