Amazon Interview Question for Software Engineer / Developers






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

reverse(node n) {
  node prev = null;
  while(n != null) {
    node next = n->next;
    n->next = prev;
    prev = n;
    n = next;
  }
  return prev;
}

- Thiyaneshwaran S November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Only one comment: since the function returns the value it should be declared as

node reverse(node n);

- Aleksey.M January 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node *reverseList(node *start, node *prev)
{
	if (start == NULL)
		return prev;
	node *temp = start->next;
	start->next = prev;
	return reverseList(temp,start);
}

/* Call as reverseList(start,NULL); */

- CyberS1ren November 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why would this not work? i.e if i just remove the remove statement

node *reverseList(node *start, node *prev)
{
	if (start == NULL)
		return prev;
	node *temp = start->next;
	start->next = prev;
	reverseList(temp,start);
}

- Joshua December 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <malloc.h>

struct node {
node *next;
int data;
}*head;

node* reverseList(node *);
void printList(node *);
node* reverseListSimple(node *);

int main() {
node *temp, *prev=NULL;
//node *head = NULL;

for(int i=0;i<10;i++) {
// create temp node...
temp = (node *)malloc(sizeof(node));
temp->data = i;
temp->next = NULL;

if(head == NULL){
head = temp;
prev = temp;
}

prev->next = temp;
prev = temp;
}

//head = reverseList(head);
head = reverseListSimple(head);

printList(head);
}

node* reverseList(node *head) {
node *revHead = NULL;

if(head->next == NULL) return head;
revHead = reverseList(head->next);

head->next->next = head;
head->next = NULL;

return revHead;
}

node* reverseListSimple(node *head) {
node *cur=head, *next, *prev=NULL;

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

return prev;
}

void printList(node *head) {
while(head!=NULL) {
printf("\n %d ",head->data);
head = head->next;
}
}

- master December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* link_reverse(Node* head)
{
if(head->next==NULL)
return head;
Node* new=link_reverse(head->next);
head->next->next=head->next;
head->next=NULL;
return new;
}

- DinakaranGct December 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess there is a correction... and it should be
Node* link_reverse(Node* head)
{
if(head->next==NULL)
return head;
Node* new=link_reverse(head->next);
head->next->next=head;
head->next=NULL;
return new;
}

- Anonymous May 19, 2010 | 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