Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

// Recursive code
Node Reverse(Node current, Node pre)
{
	Node next=current.next;
	current.next=pre;
	
	if(next==null)
             return current;

	return Reverse(next,current);
}

Node Reverse(Node head)
{
        if(head==null)
               return null;

	return Reverse(head,null);
}

//Iterative code
Node ReverseIterative(Node head)
{
	if(head==null)
		return null;
		
	Node pre=null;
	Node current=head;
	
	while(current!=null)
	{
		Node next=current.next;
		current.next=pre;
		
		pre=current;
		current=next;
	}
	
	return pre;	
}

- jie.jeff.li December 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

void RecursiveReverse(struct node** root ) {
	if (*root == NULL) 
			return;
	
	struct node* cur;
	struct node* rest;
	
	cur = *root;
	
	rest = cur->next;
	
	if (rest == NULL) 
			return;
	
	RecursiveReverse(&rest);
		
	// put the cur elem on the end of the list	
	cur->next->next = cur; 
	cur->next = NULL;
		
	// fix the root poniter
	*root = rest; 
}


void Reverse(struct node** root) {
	struct node* result = NULL;
	struct node* cur = *root;
	
	while (cur != NULL) {
		struct node* next = cur->next;		
		
		cur->next = result;
		result = cur;
		
		cur = next;
	}
	*root = result;
}

- R@M3$H.N December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

// Recursion

Node Reverse(Node current, Node pre)
{
Node next=current.next;
current.next=pre;

if(next==null)
return current;

return Reverse(next,current);
}

// iterative

public void Reverse(Node head)
{
Node NextNodeOfCurrent, Prev,Current;

Current = head;
Prev = null;

while(Current !=null)
{
NextNodeOfCurrent = Current.next;
Current.next=Prev;
Prev = Current;
Current=NextNode;
}
}

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

in Reverse iterative Method , i forgot to set ( head = Prev) after while loop

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

recursive:

rev(node* c, node *n)
{
    if n== null
       return;
    rev(n, c->next);
    n->next = c;
}

complexity => O(n)

- shivkalra1 December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse the list and return the new head's pointer:

Node* reverse(Node* pre, Node* p)
{
    Node* after = p->next;
    p->next = pre;
    if(after == NULL) return p;
    else return (p, after);
}
Node* reverse(Node* head)
{
    if(head == NULL || head->next == NULL) return head;
    else return reverse(NULL, head);
}

- uuuouou December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive:

1) Divide the list in two parts - first node and rest of the linked list.
2) Call reverse for the rest of the linked list.
3) Link rest to first.
4) Fix head pointer

void recursiveReverselinkedlist(struct node** head)
{
struct node* first_node;
struct node* rest_nodes;

/* empty list */
if (*head == NULL)
return;

first_node = *head;
rest_nodes = first_node->next;

/* List has only one node */
if (rest_nodes == NULL)
return;

/* reverse the rest list and put the first element at the end */
recursiveReverse(&rest_nodes);
first_node->next->next = first_node;
first_node->next = NULL;
*head = rest_nodes;
}

Time Complexity: O(n)
Space Complexity: O(1)

- cprof.pv1990 December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node reverse(Node head){
	Node temp = null;
	Node next = null;
	while(head != null){
		next = head.getNext();
		head.setNext(temp);
		temp = head;
		head = next;
	}
return temp;
}

- Rakesh Roy December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in a recursive program time complexity=O(n) and space also O(n) , as for every node we creating a stack (implicit stack) , while in iterative time=O(n) and space =O(n)

- shailu December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Iterative solution in java:-
class node{
private int val;
node next;
public node(int val){
this.val=val;
next=null;
}
public int getval(){
return this.val;
}

}
class linklist{
node start;
node last;
public linklist(){
start=null;
last=null;
}
public boolean isempty(){
return start==null;
}
public void insert(int val){
node newnode=new node(val);
if(start==null){
start=newnode;
last=newnode;

}
else{
last.next=newnode;
last=newnode;
}
}
public node listreverse(){
node middle=null;
node tail;
if(isempty()){
System.out.println("list empty");
return this.start;
}
else{
while(start!=null){
tail=middle;
middle=start;
start=start.next;
middle.next=tail;
}
return middle;
}

}

}
public class List_reverse {

public static void main(String[] args) {
// TODO Auto-generated method stub
linklist l=new linklist();
l.insert(10);
l.insert(20);
l.insert(30);
l.insert(40);
node a;
a=l.listreverse();
while(a!=null){
System.out.println(a.getval());
a=a.next;
}

}

}

- @@@@@@@ December 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node * reverse(node *h)
{
  node *prev=h,*future;
  if(h->next==NULL)
    return h;

  h=h->next;
  prev->next=NULL;
  while(h!=NULL)
    {
      future=h->next;
      h->next=prev;
      prev=h;
      h=future;
    }
  return prev;


}

O(n) complexity

- akash.mauryakgp December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

recursive: time = O(n), space = O(n)
iterative: time = O(n), space = O(1)

Node* recursicve_reverse(Node* node)
{
    if (node == NULL) return NULL;
    if (node->next == NULL) return node;
    
    Node* head = recursive_reverse(node);
    
    node->next->next = node;
    node->next = NULL;
    
    return head;
}

Node* iterative_reverse(Node* node)
{
    Node* head = NULL;
    while (node != NULL)
    {
        Node* tmp = node->next;
        node->next = head;
        head = node;
        node = tmp;
    }
    return head;
}

- ezra December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class node{
int value;
node next=null;
node(int v){
this.value=v;
}
void appendToTail(int n){
node end=new node(n);
node current=this;
while(current.next!=null){
current=current.next;
}
current.next=end;
}
}
public class reverse {
public static void main(String[] args) {
// generate a single list
node head=new node(Integer.parseInt(args[0]));
for(int i=1;i<args.length;i++)
head.appendToTail(Integer.parseInt(args[i]));

head=reverseLinkedList(head);
System.out.println("The result is:");
while(head!=null){
System.out.print(head.value);
head=head.next;
}
}
static node reverseLinkedList(node head){
node newHead=head;
if(head==null||head.next==null)
return head;
else{
newHead = reverseLinkedList(head.next);
head.next.next=head;
head.next=null;
return newHead;
}
}
}

- Anonymous December 26, 2013 | 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