Amazon Interview Question for Software Engineer / Developers






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

recursive approach :
struct node *reverse(struct node *p)
{
static struct node *headnode=NULL;
static struct node *reversehead=NULL;
struct node *q;
if(headnode == NULL)
{
headnode=p;
}
if(p->next == NULL)
return p;
}
q=p;
p=reverse(p->next);

if(reversehead == NULL)
{
reversehead = p;
}
p->next=q;
if(q == headnode)
{
q->next == NULL;
return reversehead;
}
retun q;
}

Pls give ur feedback .

- tom September 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RecursiveReverse(struct node** headRef) {
struct node* first;
struct node* rest;
if (*headRef == NULL) return; // empty list base case
first = *headRef; // suppose first = {1, 2, 3}
rest = first->next; // rest = {2, 3}
if (rest == NULL) return; // empty rest base case
RecursiveReverse(&rest); // Recursively reverse the smaller {2, 3}
// after: rest = {3, 2}
// put the first elem on the end of the list
first->next->next = first;

first->next = NULL; // (tricky step -- make a drawing)
*headRef = rest; // fix the head pointer
}

- mshah2 September 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Reverse(struct node** headRef) {

struct node* result = NULL;

struct node* current = *headRef;

struct node* next;

while (current != NULL) {
next = current->next; // tricky: note the next node
current->next = result; // move the node onto the result
result = current;
current = next;
}
*headRef = result;
}

- mshah2 September 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the recurse version is awkward because the problem in nature is not recursive. what is the point

- Jackie September 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

maybe not.
You can think like that:
For the nth node, from n+1 th node to the last node are all reversed, you just need to turn n+1 th node's next pointer to nth.

- sunbow.xs@hotmail.com September 29, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Item* Reverse(Item* head)
{
if (!head || !head->next)
return head;
Item* last = head;
Item* newHead = head->next;
last->next = NULL;

while (newHead)
{
Item* temp = newHead;
newHead = newHead->next;
temp->next = last;
last = temp;
}
newHead = last;
return newHead;
}

Item* Reverse2(Item* head)
{
if (!head || !head->next)
return head;

Item* temp = head->next;
Item* newHead = Reverse2(head->next);
temp->next = head;
head->next = NULL;
return newHead;
}

- sunbow.xs@hotmail.com September 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Recursive Method
revers_list(NULL,head);
reverse_list(struct node *curr1,struct node *curr2)
{
if(curr2==NULL)
{
head=curr1;
return;
}
reverse_list(curr2,curr2->next);
curr2->next=curr1;
}

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

PNode RecurReverseLink( PNode L )
{
if( L == NULL )
{
return L;
}
if( L->next == NULL )
{
return L;
}
PNode p = L->next;// tricky,p holds the tail of sub-link
PNode newHead = RecurReverseLink( L->next );
p->next = L;
L->next = NULL;
L = newHead;
return L;
}

- andy October 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one andy !

- Anonymous December 25, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static reverse(LinkedlistNode head){
LinkedListNode before=null, current=head.next, next;
while(current!=null){
next=current.next;
current.next=before;
before=current;
current=next;
}
return before;
}

- iterate way March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry this should be right:


public static reverse(LinkedlistNode head){
LinkedListNode before=null, current=head, next;
while(current!=null){
next=current.next;
current.next=before;
before=current;
current=next;
}
return before;
}

- Anonymous March 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

//Iterative Method
curr1=head->next;
head->next=NULL;
while(curr1!=NULL)
{
curr2=head;
head=curr1;
curr1=curr1->next;
head->next=curr2;
}

- Anonymous October 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have not set head position afterwards.Also next pointer of your first node should be null

- ankuragrawal420 November 15, 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