Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

reverse function will reverse the entire linked list
reverseK function will reverse by every k nodes
/**
 *
 * @author Sun
 */
public class reverseList {
    public static node reverse(node head){
        node cur,next;
        if(head==null || head.next==null)return head;
        cur=head.next;
        next=cur.next;
        head.next=null;
        while(next!=null){
            cur.next=head;
            head=cur;
            cur=next;
            next=cur.next;
        }
        cur.next=head;
        head=cur;
        return head;
    }
    public static node reverseK(node head,int k){
        if(head==null || head.next==null || k<=1) return head;
        node cur,next,tail=null;
        node newhead=null;
        while(true){
            cur=head;
            for(int i=0;i<k-1;i++){
                if(cur.next==null)break;
                cur=cur.next;                
            }
            next=cur.next;
            cur.next=null;
            reverse(head);
            if(newhead==null)newhead=cur;
            if(tail!=null){
                tail.next=cur;
            }
            tail=head;
            if(next==null)break;
            head=next;
        }
        return newhead;
    }
    public static void printList(node head){
        node cur;
        cur=head;
        while(cur!=null){
            System.out.println(cur.number);
            cur=cur.next;
        }
    }
}

- Test May 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Got it, the whilecondition should be while(!isEmpty(head)), the '!' was missing.
Also need to change head to point to the new head. So after the loop finishes, I need to add
head=previous; the new reverse function would be

void reverse()
{
revhead = head;
link previous=null,temp=null;
while(!isEmpty(revhead))
{
temp = revhead.nextlink;
revhead.nextlink = previous;
previous = revhead;
revhead = temp;
}
//To make head point to the new head, which was the last element earlier
//but now after reversing is the head
head = previous;
}

- chaos May 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def reverse(node):
  curr = node
  prev = None
  while curr is not None:
    next = curr.next
    curr.next = prev
    prev = curr
    curr = next

def reverse_every_k(node, k):
  curr = node
  prev = None
  i = 0
  while curr is not None:
    next = curr.next
    if i != 0 and i % k == 0:
      curr.next = prev
    prev = curr
    curr = next

- Martin May 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Forgot the increment of "i"

- Martin May 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Missing things...
1. increament of i
2. i should be initialized to 1
3. check if k ==0 ...before while....

- virgo676 May 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This logic does not seem to work

- chaos May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/python

class MyList:
    class ListNode:
        def __init__(self,value):
            self.value=value;
            self.next = None;

    def __init__(self):
        self.head=None;

    def append(self,value):
        node = self.ListNode(value)
        if (self.head==None):
            self.head = node
        else:
            node.next = self.head
            self.head = node

    def reverse(self):
        cursor = self.head
        self.head = self.head.next
        cursor.next = None
        while self.head.next:
            temp = self.head.next
            self.head.next = cursor
            cursor = self.head
            self.head = temp
        self.head.next = cursor

    def __str__(self):
        cursor = self.head
        string =  '( '
        while cursor:
            string += "{} ".format(cursor.value)
            cursor=cursor.next
        string += ')'
        return string

list = MyList()
list.append(1)
list.append(2)
list.append(3)
list.append(4)
list.append(5)

print list
list.reverse()
print list

- btolnas May 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am going to explain the basic concept, coding it should be easy, to reverse a list such as
1->2->3->4.
Curr=Head;
Last=Head;
1).Get a pointer to the last element, by traversing till the end
2). While(Curr!=Last)
4). temp = curr;
5). If Last.Next!=null, than temp.next=Last.Next;
6).Last.Next = Temp;
7).End While
8)Head=Last;

1-2-3-4
Last = 4;
2-3-4-1
3-4-2-1
4-3-2-1
Head = 4

- pga June 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void kreverse(int len)
{
	node *st=start,*p=start,*q=p->next,*r=q->next;
	int i=0;
	while(i<len)
	{
		i++;
		q->next=p;
		p=q;
		q=r;
		r=r->next;
	}
	st->next=q;
	start=p;
}

- varun July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

len is the no of elements from front to be reversed.
start is the head pointer .

- varun July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct node * reverse(struct node *prev,struct node * curr,struct node *next)
{
	if(curr!=NULL)
	{
		curr->link=prev;
		prev=curr;
		curr=next;
		if(next==NULL)
			return prev;
		else
		{
			next=next->link;
			reverse(prev,curr,next);
		}
	}
}

- Anonymous December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

call function in main with arguments as reverse(NULL,head,head->link);

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

def reverse_iterative(self):
        last = None
        current = self.head
        while current:
            nxt = current.next
            current.next = last
            last = current
            current = nxt

        node = last
        while node:
            print node.value,
            node = node.next

- Arunprasath Shankar February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't understand why its not working.
i tried a lot but could not find the bug . i am reversing a list in blocks of nodes.
here is my code:

int hasnode(struct node *s,int k)    //checks if k node exist 
{
	int i;
	for(i=1;s&&i<k;i++)
	s=s->next;
	if(i==k)
	return 1;
	else
	return 0;
}
struct node* getkplusoneth_node(struct node **nextnode,int k) //returns k+1th node
{
	
	int i;
	for(i=0;*nextnode&&i<k;i++)
	*nextnode=*nextnode->next;
	if(i==k)
	return *nextnode;
		
}	
void reverseinkblocks(int k)    //reverse function
{
	int i;
	struct node* prev=NULL,*current=head,*nextnode=current,*temp;
	while(current&&hasnode(current,k))
	{
		*nextnode=getkplusoneth_node(&nextnode,k);
		
		
		while(current!=nextnode)          //reverse upto one node before nextnode
		{
			temp=current->next;
			current->next=prev;
			prev=current;
			current=temp;
		}
	}

}

- Devesh prasad March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse_diffn(dnode **dpnode)
{
	dnode *curnode = *dpnode;
	unsigned int prevaddr = NULL, curraddr = NULL;

	while (curnode != NULL)
	{
		curraddr = (unsigned int)curnode;
		curnode = curnode->down;
		*((unsigned int *)curraddr + 1) = (prevaddr);
		prevaddr = curraddr;
	}
	*dpnode = ((dnode *)curraddr);
}

- vlsireddy October 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Take a closer look at Reverse() Walk through, be the computer...

- Sarge May 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assign the list to the temp var lList and write the remaining code. It works fine...
Node lList=head;
while(lList!=null){
Node temp=lList;
lList=lList.next;
if(head==temp){
temp.next=null;
}else{
temp.next=head;
}
head=temp;
}

- Devakumar Jayaraman May 29, 2012 | 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