Amazon None Interview Question for Nones


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
4
of 8 vote

I am assuming that list's head is pointing the start of queue (so the front) and tail is pointing the end of queue (so the rear as first in first out). So if the list is circular, then if we have a pointer that points to the head, then for enqueue operation, we have to traverse to the end of the list and then can add a new element (I am assuming list in single linked list). But if we have a pointer at tail, then for enqueue we can do that O(1) by newnode->next=tail->next->next; tail->next=newnode; tail=newnode;
For dequeue, We can do that in O(1) too, newnode=tail->next; tail->next=tail->nex->next; return newnode;

So answer should be rear.

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

I guess the answer should be the "node before the front node" of the queue, since for deletion in O(1) you need node before front , since list is circulary connceted so rear can also be approached by this node so both insertiona nd deletion will be O(1

- zeroByzero December 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

1) Rear.
As the rear pointer is the pointer can access the top and the bottom of this list in constant time of 1. All other node is depending on the length of this list and its position. The worst case is the front because it needs to go through the whole list to reach the last node.

And enqueue/dequeue is an operation on top/bottom node.

- peter tang October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

rearP; // pointer pointing to the rear node
------ enqueue ------
newNode->next = rearP->next;
rearP->next = newNode;

------ dequeue -------
topP = rearP->next;
rearP->next = top->next;
return topP

So the answer is '''rear'''.

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

you need front of queue (i.e end of the circular LL which point to first of the LL).
enque: (insert at node next to p)
Node n = new Node();
n.next = p.next;
p.next = n;
deque: (Delete the node at p)
p.data = p.next.data;
p.next = p.next.next;

- kapad October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I guess the answer should be the "node before the front node" of the queue.

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

yes,
since for deletion in O(1) you need node before front , since list is circulary connceted so rear can also be approached by this node so both insertiona nd deletion will be O(1)

- zeroByzero December 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

folks what do we mean by taking O(1) time. what does that exactly mean?

- sastry.soft October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You want a pointer to the last item.

Suppose three items in the queue.
- Item #1 points to item #2
- Item #2 points to item #3
- Item #3 points to item #1

To enqueue a new item (item #4), we need to change item #3 to point to item #4.

Once item #4 is added, to dequeue item #1 we need to change item #4 to point to item #2.

For both enqueue and dequeue, we're updating the last item in the list, so having a pointer to the last item in the list is most efficient.

- Greg Atkinson October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it is option D which could be node before rare that is answer_node->next=rare.
Cause for
1.add operation on queue you have to do
answer_node->next->next=new_node
answer_node = answer_node->next
2. To remove from front
answer_node->next->next = head -> next
new_head = head -> next
delete(head)
head=new_head


If the rare node is consider as answer we have to traverse whole link list to dlete the rare node to perform delete operation.

- Dheeraj Pande March 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it is option D which could be node before rare that is answer_node->next=rare.
Cause for
1.add operation on queue you have to do
answer_node->next->next=new_node
answer_node = answer_node->next
2. To remove from front
answer_node->next->next = head -> next
new_head = head -> next
delete(head)
head=new_head


If the rare node is consider as answer we have to traverse whole link list to delete the rare node to perform delete operation.

- Dheeraj March 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If anyone tried to find out that

If there is only 1 element in list i.e. pointing to itself condition then you cant perform deletion.

So answer is c) not possible

- Shantanu Agarwal December 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think using 1 ,2 ,3 u can do it

- Anonymous October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

i think it would be front bz when v r talking about circular list so if front is assigned a pointer , v cn come to rear by just finding next pointer to the next node .

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

Yes...
it is the front of the queue as we can use the circular linked list
1)we can enqueue the element at the rear by bellow
newNode->next = front->next ;
front->next = newNode;
newNode->data = front->data;
front->data = givenDataToEnqueue;
front = newNode;

2)To Dequeue:
As we are enqeuing from the rear by above snippet ..
For deqeue we need to do it from front.
nextFrontNode = front->next;
front->data = nextFrontNode->data;
front->next = nextFrontNode->next;
free(nextFrontNode);

- Kartheek J October 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dequeue is not correct..

- BJ October 14, 2012 | 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