Amazon None Interview Question
NonesCountry: India
Interview Type: Written Test
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.
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.
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.
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.
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 .
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);
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;
- arwin October 13, 2012For dequeue, We can do that in O(1) too, newnode=tail->next; tail->next=tail->nex->next; return newnode;
So answer should be rear.