Amazon Interview Question for Software Engineer / Developers






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

Standard trailing pointer trick.

- Anonymous March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

more details?

- wyse March 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

just easy one by use of the trailing pointers technique use this code for this one:
struct node *Nthlast(struct node *L,int n)
{
int c=0;
struct node *temp=L,*temp2=L2;
if(!L)
return L;
while(c!=n-1 && temp1!=NULL)
{
c++;
temp1=temp1->next;
}
if(temp1==NULL)
return NULL:

while(temp1->next!=NULL)
{
temp2=temp2->next;
temp1=temp1->next;
}

return temp2;

}

- geeks August 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

two traversals to get it done!

1st, reverse the list to the new list
2nd, from the previous list's last one, we reverse this new list back to original order, at the same time we count the number of current handling node, when it's nth , we print it out.

ele findLastNth(node *head, int n)
{
int i;
node *p, *q ,*cur;
p=cur=head;
q=p->next;
p->next=NULL;
while(q!=NULL){
cur=q;
q=q->next;
cur->next=p;
p=cur;
}
q=p->next;
p->next=NULL;
i=1;
while(q!=NULL){
cur=q;
q=q->next;
cur->next=p;
if(i==n)
return p->ele;
p=cur;
i++;
}
}

- lyzoridc March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

one traverse should be enough by keeping a count how many first pointer is ahead of the second one.

- Anonymous March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can just traverse to the end of the list and we will get the nth last element right? Why are you reversing it in the first place? not required i think.

- jango March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. check size of the LL

2. run two pointers
one i++
other J+=2
when j reaches N , i will be at Center
then we know that there is equal distance from N now increment to the asked[Kth last element]

- Null March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

run 2 pointers , first one -k places the second one when first will reach end second one will be at the kth last place

- Tarun Phaugat March 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Exactly........I think this is the best algo for this....

- KK July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node *A, *B

A = head
B = head

for i in [0 to n-1]
if(!A) return -1
A=A->next

while (!A)
A=A->next
B=B->next

return B

- Anon March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* NthLast(Node *root,int n)
{
      int i=0;
      Node *nth,*last;
      last = root;
      while(i< n)
      {
              last = last->next;
              i++;
      }


      nth = root;
      while(last != NULL)
      {
              last = last-next;
              nth = nth->next;
      }
      return nth;

}

- andy March 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

[1].By recursion.
[2].Take two pointers.First will point to first node and second will point to nth node from the first.we will move both pointers untill we get NULL at second pointer.thats all first pointer is pointing to you ANS(nth element from the last).

- PKT February 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ask if its a doubly linked list, if so just traverse to the anthem element backwards .

- somu November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void findNthFromLast(struct Node* headNode, int n)
{
/*this has to be static so that it holds it's value from
call to call because of the fact that a non-static variable
will not hold it's value since the variable will just be local
to the function if it's not static and will not be preserved
across call stack: */

static int i = 0;
//base case when we reach the end of linked list:
if(headNode == NULL)
return;
//this is where head pointer is advanced (head->next)...
findNthFromLast(headNode->next, n);
//increment i and check to see if equals n
//if it does equal n then print out the element's data
if(++i == n)
printf("%d", headNode->elementData);
}

- lida September 14, 2017 | 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