Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

It's simple, it starts pointing to the end of the list, try adding one more value to this list with an insert statement, and then you won't get this error.

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

Iterative version::

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linkedlist{
	int val;
	struct linkedlist *next;
}ll;

ll *start;

ll *add_node_last(ll *start,int value){
	ll *temp=NULL,*temp1=NULL;
	if(start!=NULL){
		temp=malloc(sizeof(ll));
		temp1=start;
		temp->val=value;
		temp->next=NULL;
		while(temp1->next!=NULL)
			temp1=temp1->next;
		temp1->next=temp;
		return start;
	}
	else{
		start=malloc(sizeof(ll));
		start->val=value;
		start->next=NULL;
		return start;
	}
}

void print_ll(ll *start){
	ll *temp=NULL;
	temp=start;
	while(temp!=NULL){
		printf("%d->",temp->val);
		temp=temp->next;
	}
	printf("\n");
}

ll *reverse_k_element(ll *start,int k){
	ll *t=NULL,*t1=NULL,*t2=NULL,*start1=NULL,*p=NULL;
	int count=0;
	int i=1;
	t=start;
	t2=start;
	while(1){
		while(i<k&&t->next!=NULL){
			t1=t->next;
			t->next=t1->next;
			t1->next=t2;
			t2=t1;
			i++;
			if(p!=NULL){
				p->next=t1;
			}
		}
		if(i==k){
			p=t;
			count++;
			if(count==1){
				start1=t2;
			}
			t=t->next;
			t2=t;
			i=1;
		}
		else if(count>=1)
			return start1;
		else
			return t1;
	}
	return t;
}
	
main(){
	start=NULL;
	ll *temp=NULL,*temp1=NULL;
	int n=8,i=1,k=3;
	//start=malloc(sizeof(ll));
	while(i<=n){
		start=add_node_last(start,i);
		i++;
	}
	temp=reverse_k_element(start,k);
	print_ll(temp);
}

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

Recursive version

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linkedlist{
	int val;
	struct linkedlist *next;
}ll;

ll *start;

ll *add_node_last(ll *start,int value){
	ll *temp=NULL,*temp1=NULL;
	if(start!=NULL){
		temp=malloc(sizeof(ll));
		temp1=start;
		temp->val=value;
		temp->next=NULL;
		while(temp1->next!=NULL)
			temp1=temp1->next;
		temp1->next=temp;
		return start;
	}
	else{
		start=malloc(sizeof(ll));
		start->val=value;
		start->next=NULL;
		return start;
	}
}

void print_ll(ll *start){
	ll *temp=NULL;
	temp=start;
	while(temp!=NULL){
		printf("%d->",temp->val);
		temp=temp->next;
	}
	printf("\n");
}

ll *reverse_k_element(ll *start,int k){
	ll *t=NULL,*t1=NULL,*t2=NULL,*start1=NULL,*p=NULL;
	int count=0;
	int i=1;
	t=start;
	t2=start;
	while(1){
		while(i<k&&t->next!=NULL){
			t1=t->next;
			t->next=t1->next;
			t1->next=t2;
			t2=t1;
			i++;
			if(p!=NULL){
				p->next=t1;
			}
		}
		if(i==k){
			p=t;
			count++;
			if(count==1){
				start1=t2;
			}
			t=t->next;
			t2=t;
			i=1;
		}
		else if(count>=1)
			return start1;
		else
			return t1;
	}
	return t;
}
	
main(){
	start=NULL;
	ll *temp=NULL,*temp1=NULL;
	int n=8,i=1,k=3;
	//start=malloc(sizeof(ll));
	while(i<=n){
		start=add_node_last(start,i);
		i++;
	}
	temp=reverse_k_element(start,k);
	print_ll(temp);
}

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

sorry here is the recursive version:::

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linkedlist{
	int val;
	struct linkedlist *next;
}ll;

ll *start;

ll *add_node_last(ll *start,int value){
	ll *temp=NULL,*temp1=NULL;
	if(start!=NULL){
		temp=malloc(sizeof(ll));
		temp1=start;
		temp->val=value;
		temp->next=NULL;
		while(temp1->next!=NULL)
			temp1=temp1->next;
		temp1->next=temp;
		return start;
	}
	else{
		start=malloc(sizeof(ll));
		start->val=value;
		start->next=NULL;
		return start;
	}
}

void print_ll(ll *start){
	ll *temp=NULL;
	temp=start;
	while(temp!=NULL){
		printf("%d->",temp->val);
		temp=temp->next;
	}
	printf("\n");
}

ll *reverse_k_element(ll *start,int k){
	ll *t=NULL,*t1=NULL,*t2=NULL,*start1=NULL,*t3=NULL;
	int i=1;
	if(start==NULL)
		return NULL;
	else if(start->next==NULL)
		return start;
	else{
		t=start;
		t2=t;
		while(i<k&&t->next!=NULL){
			t1=t->next;
			t->next=t1->next;
			t1->next=t2;
			t2=t1;
			i++;
		}
		if(t->next!=NULL){
			t3=reverse_k_element(t->next,k);
			t->next=t3;
			return t2;
		}
		else
			return t2;
	}
}
	
main(){
	start=NULL;
	ll *temp=NULL,*temp1=NULL;
	int n=8,i=1,k=3;
	//start=malloc(sizeof(ll));
	while(i<=n){
		start=add_node_last(start,i);
		i++;
	}
	temp=reverse_k_element(start,k);
	print_ll(temp);
}

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

{
void reverse(int x){// x is the position that is kth element
revhead=head;
link current = revhead;
link head11 = null;

for(int i=0;i<x && current!=null;i++)
{
link save = current;
current = current.nextlink;
save.nextlink = head11;
head11 = save;

}
while(head11!=null){
System.out.print(" Data "+head11.data);//printing the new reverse list
head11 = head11.nextlink;
}
System.out.println();


}
}

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

Please comment on below code if you can suggest to improve it more...I am new to C++

void lList::reverseKelement(int k)
{
list *temp = l;
list *temp1 = NULL;
list *temp2 =NULL;

while(k!=0)
{
temp2 = temp->next;
temp->next = temp1;
temp1 = temp;
temp = temp2;
k--;
}
l->next = temp;
l = temp1;

}

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

This will only work if we need to reverse k elements and there are K elements in the list but not for multiples of K.
Eg. If list has 5 elements and K is 5, this will work, but if we have 10 elements and we need to reverse every 5 elements, this wont work, trying coding it, you will understand

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

int length = getLenghtByTraversing();
truncation = length%k;
division= length/k;

for(int i=0;i<division;i++){

if(ptr3==null)
ptr1 = rootNode;
else
ptr1=ptr3;

ptr2= ptr1->link;
ptr3=ptr2->link;

for(int j=0;j<k;j++){
ptr2->link=ptr1;
shift(ptr1,prt2,ptr3);
}
}

- lohith July 03, 2012 | 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