Amazon Interview Question for Software Engineer / Developers






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

It was a phone interview. i proposed using a hash table with the priority as the key and each cell storing a queue of nodes. I could not solve the dequeue part in O(1) time. The interviewer mentioned that i was almost there. i havent figured out the answer though.

- vairaghi December 12, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello Vairaghi,
For Dequeue we can may be maintain an int which basically tells us the current highest priority. If a new element comes in with higher priority it needs to be updated. So when we dequeue we check this int and find the current highest priority. Using this we get directly mapped to the bucket which stores a Queue. Now our queue structure can have a first and last pointers which points us to the first and last elements in the queue. Instead of queue we can also use a doubly linked list. so dequeuing is just removing the head element and updating the head pointer of the queue.
I guess this solution will be O(1) dequeue.

Any comments people......

- AJ February 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Vairaghi, what was your solution to this question?
Was it an on-site interview or phone interview?
Thank you.

- sunny December 10, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For dequeue:

Checking the end of a list can be done in O(1). Checking the next priority is also O(1); the number of priorities is constant so the iteration will be O(1) or O(num priorites); essentially constant time.

- Jack December 13, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use heap to implement the priority queue, also have a variable to keep track of the maximum priority in the query. For every insertion, only compare the inserted element with that variable, thereby reducing the number of comparisons.

- AmazonWannaBe January 31, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

suppose if we want to develop this algo in JAVA, do we have any API for Heap datastructure or we should create our own queue ADT to implement priority Queue?? i m just a novice java programmer. Can u help me to figure this.?
if time permits plz mail me at arvinthdd@gmail.com
thanks in advance.

- arvind May 15, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this is how the Linux2.6 kernel does it:
Have a doubly-linked list for each priority level. The head of each linked list can be stored in a hash, key's being the priority level, values being the head of the list.

- Koonz February 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I completely agree with this solution.

- Daniel Johnson February 11, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now that I think about it, you don't even need a hash. Since we are dealing with priority levels here, you can just have an array of size N where N is all the priority levels.

- Koonz February 14, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using linked list and hash map together ? i.e. a linked list to enqueue in O(1) time and while dequeing, find the element in O(1) time using hash map which has the address of the lined list and address of node. You delete the node in O(1) time.

So essentially we can have both the operations in constant time.

Please correct me if I am wrong. Your comments are appreciated.

- Daniel Johnson February 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems right!

- Anonymous January 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the main is to support enqueue and dequeue in O(1) and get the highest pri in O(1)...will this work...

keep 2 queues...
one queue where we add from one end and remove from other...call this Q...
another whose top always has the highest pri element...call this PQ

here are add, delete and getHighPri methods...


enquee(obj o)
{
Q.addFront(o);

if (PQ.isEmpty())
PQ.addFront(Q)
else
if ( o.pri > PO.front().pri)
PQ.clear
PQ.add(o);
else
PQ.add(o)

}


getHighestPri()
{
return PQ.front()
}

dequeue()
{
object todeque = P.dequeue;
if (todeque == PQ.front)
PQ.dequeue;
}




p 10 11 9 15
pq 9 15

- king_k July 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do you ensure O(1) insertion for PQ?

- geek January 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Use a singly linked list to represent the queue that would hold the items according to their priorities.

# Since we have constant number of priorites (say 1 to 10), we can have an array of structure(of the type mentioned below) that would give us the hint where to insert an item in the linked list.

struct sub_pq {
Node* first;
Node* last;
};


Enque: Look in the array for 'last' node for the given priority, and insert next to it. If there is no item with this priority already in the list, then look for 'last' of higher priorities and/or 'first' of lower priotities, and then insert the item at it's appropriate position.
Then update the corresponding array element accordingly. O(1)
Deque: Remove first item from the linked list, and update the corresponding array element accordingly. O(1)

- -- February 20, 2012 | 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