Amazon Interview Question for Software Engineer / Developers


Country: United States




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

if i understand your question you mean like
if input is 10->20->30->40->50
then output should be like 30->40->50->10->20 (for k =3)

if this is the case then this can be done finding the 3rd node in the list from the last

so in our case it is 30 and the last node we have is 50. now simply we need to set the last node next to head and previous valus of the 3rd node from the last that is 20 should be set to NULL . now make the 3rd node from the last that is 30 to be the head of the link list.

- Anonymous October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Anonymous,

Even I expected the output to be straightforward as you mentioned above, like
30->40->50->10->20

but this wasn't the case in the question. It was in random order like I mentioned in the question which made me little puzzled.

I think, perhaps we have to do multiple such rotations for K times to arrive at the solution..

- gdsrinivasan October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct Node * swap(struct Node *TT)
{
    struct Node *L, *t;
    if(TT)
    {
    if((TT->nxt)->nxt!=NULL)
    {
      L=(TT->nxt)->nxt;
      printf(" %d  ",L->val);
      t=swap((L->nxt)->nxt);
      (L->nxt)->nxt=TT;
      (TT->nxt)->nxt=t;
      return L;
    }
    else
    {
      return TT;
    }
    }
    else
      return NULL;   
}

- Anonymous October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

printf line is unnecessary remove it....

- Anonymous October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok

- Anonymous October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your result is 30 40 10 20 70 80 50 60
not correct?

- Anonymous October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

_myList * rotateList(int i, _myList *node)
{
	static _myList*retNode=NULL;

	if(i == 0 || node == NULL)
		return retNode;
	else
	{
		_myList *elementToAdd=node;
		node=node->next;
		elementToAdd->next=NULL;

		retNode=node;

		while(node->next) node = node->next;
		node->next=elementToAdd;
		
		rotateList(--i, retNode);

	}
}

- naren October 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am confused here. May be have not understood the question. When I rotate anything it will end up with the same data, in same order. Please let me know if we are talking about shifting instead of rotating.

- Sunil December 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes Sunil... as we see in my question, 30 becomes the head node after shifting.

- gdsrinivasan January 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

may be the question is some thing like this (I got this below question while reading some other post. Pasting here as you said you are not sure what was the question)

FYR,
Reverse a Linked List in groups of given size
Given a linked list, write a function to reverse every k nodes (where k is an input to the function).

Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.

Inputs: 1->2->3->4->5->6->7->80->NULL and k = 5
Output: 5->4->3->2->1->8->7->6->NULL.

- Sarat Chandra Sai K October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ListNode* Solution::rotateRight(ListNode* A, int B) {
ListNode* p = A;
int len=0;
while (p!=NULL){
len++;
p=p->next;
}
int r;
if (B%len==0){
return A;
}else{
r = len-(B%len)-1;
}
p=A;
while (r>0){
p=p->next;
r--;
}


ListNode *q =p->next;
if (q==NULL){return A;}
while (q->next!=NULL){
q=q->next;
}
q->next = A;
q=p->next;
p->next=NULL;

return q;
}

- Pankaj Kumar June 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ListNode* Solution::rotateRight(ListNode* A, int B) {
   ListNode* p = A;
        int len=0;
        while (p!=NULL){
            len++;
            p=p->next;
        }
        int r;
        if (B%len==0){
            return A;
        }else{
            r = len-(B%len)-1;    
        }
        p=A;
        while (r>0){
            p=p->next;
            r--;
        }
         
         
        ListNode *q =p->next;
        if (q==NULL){return A;}
        while (q->next!=NULL){
            q=q->next;
        }
        q->next = A;
        q=p->next;
        p->next=NULL;
         
        return q;
}

- Pankaj Kumar June 28, 2015 | 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