Amazon Interview Question for Software Engineer / Developers






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

shouldn't use recursion, because the stack would use too much memory.
I would do it with three pointers. Pseudo follows (assuming list ends when next points to null).

declare pointers curr, prev, next

set prev = null;
set curr = list_head;
do loop
  set next = curr->next
  set curr->next = prev
  set prev = curr
while curr != null

- zsepi March 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you forgot to add curr=next in the loop.

- Sridhar March 04, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops. Thanks for the correction!

- Anonymous March 05, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi
structure of the linklist

struct lnlst{
int info;
struct lnlst *next;
};



function to reverse using iteration:

struct lnlst* reverse(struct lnlst *ls)
{
struct lnlst *temp,*prev,*current;

prev=0;
temp=0;
current=ls;
while(current)
{
temp=current->next;
current->next=prev;
prev=current;
current=temp;
}
return(prev);
}

- Ravi Kant Pandey March 30, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,
this can also be written as:

struct lnlst* reverse(struct lnlst *ls)
{
struct lnlst *temp,*prev,*current;

prev=0;
temp=0;
current=ls;
if(current == NULL)
return;

do
{
temp=current->next;
current->next=prev;
prev=current;
current=temp;
}while(current)

return(prev);
}

if there is any problem plz let me know

- Ravi Kant Pandey March 30, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(struct nodes *head)
{
	struct nodes *prev, *now, *future;
	prev = head;
	now = prev->next;
	while(now)
	{
		future = now->next;
		now->next = prev;
		prev = now;
		now = future;
	}
	head->next = NULL;
	head = prev;
	display_list(head);
}

- gauravk.18 February 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

u could use recursion if u want to...
it's less efficient in memory...

node* reverse(node* list) {
node* pointer = list;
if(list->p_next!=NULL) {
pointer = reverse(list->p_next);
(list->p_next)->p_next = list;
list->p_next = NULL;
}

return pointer;
}

- colin November 07, 2008 | 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