## Amazon Interview Question

Country: United States

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

Make it available to all customers even if a customer has added one last piece of that item in his/her cart. So, allow everyone to add in their carts, If anyone proceeds to make a payment (we can capture this event), take a lock on that item. What say?

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

Its a similar argument what Alex made in previous answer. The problem here is It would be considered as bad experience, when potential customers will get message "Item no longer available", though they have added 1 in their cart.

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

since Amazon always shows "1 item left in stock" I like this approach better!

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

Say n is the number of highly rated books we are looking for and V is the number of vertices, E is the number of edges in the graph.

While running BSF, we should maintain minHeap(at max size of n). When ever we encounter a book with rating more than the min of heap tree, replace it with top.

Complexity: O((E+V)logn)

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

Thanks bharat for solution, sorry for question change. Previous question has already been answered.

Comment hidden because of low score. Click to expand.
2

It would be better not to change the question. If needed, you can edit/add more information to the existing question, but don't change it. If needed, you can always add a new question. It doesn't make sense for people whoever looks at the answer now.

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

We can have a lock for some time(T) whenever someone adds an item to cart. Before any item is being added to cart, we check whether the item is not locked.

We can define the time T based on the demand. On bonanza sale, the time T would be very less.

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

Humm... sounds good.

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

bharat, what happens when time T expires?

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

What will happen when time T expires and person X hasn't made any decision yet about purchasing that book, and book is still sitting in his cart ?

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

In a brick and mortar store if someone has the last item placed in their shopping cart the shelf will appear empty. I would follow the same model. If they decide to not purchase the item then it becomes available again.

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

I exactly answered this but the argument from interviewer was, we don't wan't to loose our potential customer. so until person x has paid money item should be available to both. That means person x can sit there putting item in his cart whole day. we've to some how solve this by some trick.

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

Yeah I figured that it wasn't the type of answer the interviewer was looking for.

Okay so let's try to solve this another way.

So based upon this info then the item becomes unavailable only after a customer has clicked on the "Pay" button. Until then it must remain in both customers shopping carts. Once one of the customers clicks on the pay button, the item should be silently removed from the other customer's shopping cart with a message "Sorry this item is no longer available... more on the way" or something of that nature.

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

Hi Alex, you won't believe me but I tried this as well and interviewer said Its really bad customer experience. He said to me that imagine as a customer you really badly want some book and only 1 piece left and you're hoping that you would get it. But when you're going to pay it says "no longer available". How would you feel ?

My next idea was creating some queuing system where It remember's person Y's order for some n days, send him reminding e-mails that you're first in the queue until new inventory is available for that book.

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

Yikes!
Alright here's another way of handling this problem.
Let's pretend there are 20 actual physical items left in stock. I'd report in the amazon UI that there are only 10 items left in stock. Once this count drops to zero, we'd have a backup of 10 more items left on shelf just to handle the scenarios where two customers have the same item in the same cart. This would mitigate the problem but not totally eliminate it. But something along these lines should be sufficient for most scenarios I believe.

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

How about displaying a simple message something like.. "This is the last item of the copy. Please grab it before anyone else takes it." and then use the locking logic mentioned above.

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

I would suggest processing the order for both the customers. There is no issue for the first customer that makes the payment. Inform about potential delay in delivery for the second customer. I am not sure on the replenishment algorithm/logic followed by Amazon...one piece of advise is to place the order for more items when stock is below the safe level

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

Single Click (TM) would solve the issue.

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

In case of payment failure for first person for which item already locked, when second person tries to make payment what would be the behaviour, as we have made this available for all ?

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

When user actually processes on Make payment , then lock the inventory with message to other users that , the last items is in process . May be you can try later. The logic is users payment might not be successful , so for time T , lock the inventory for others and after say 3 attempts unlock the inventory so that other uses can try to do payment.

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

Since the duration that the book stays in the cart is an issue, we should try to reduce it by encouraging users to buy (at least) this particular item with "One-Click". This way, a quick decision is made on purchase and no users are disappointed.

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

We need to consider two aspects
1. Inform 2nd Customer that product is not available.
2. Lock the item while actual payment being in process.

Who ever proceeds to check out page first the Item should be flagged as BLOCKED and also it should have timeout for that BLOCK. Sometime user keeps the checkout page open.

While from 2nd user checkout page if the given item is BLOCKED then display message as "Given item is temporarily unavailable. Please try later." and on the same page call async method to check the FLAG with 2 sec interval. If the 1st used didnt proceed with payment then Un block the item for second user and block it for 1st user.

This will give the good customer experience.

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

if there is 1 item in stock then allow any number of users to put it in their cart.
Only first person who orders it get it shipped, then the item availability changes to <not in stock> for all new users. For those who had it placed in their carts item is kept in their cart until the users tries to place the order. Then, at order time if any item changed state from available to <not in stock> show a warning saying: "sorry, the following items aren't in stock, do you want to back-order them?" and give user to options: place the order or modify the order.

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

I think we should have a lock time based on amount of quantity available and the customer should be notified when he adds the item to cart that the item is only guaranteed for x amount of time based on the lock time we calculated. The item should be shown as unavailable for that period of time to other customers.

Other approach is to divide the type of items into two categories, one which can be backordered if over-ordered and others which cannot be. For the 1st option, we should allow customers to order the item as long as they started the checkout process before anyone else purchased it and then provide them the backordered status. For the other type, we just need to show unavailable message if payment fails due to unavailability.

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

If the transaction is backed by the database, then the data base grantees atomicity.
Two customers can add the item in cart, back end service will get the item info and render the front end with the item information visible in the cart section.
When its time to checkout, just one of the server request will be successful. For the other request, the server can formulate a response which will cause the front end to display the appropriate message to the customer.
Additionally, the front end can use asynchronuous calls(ajax, socket io etc) to back end to periodically update/refresh with the current status (expiration time, status etc) to avoid unnecessary check out attempt.

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

How about if we do something like booking.com?

There will be some kind of pushing notification to all the clients for every actions/events taken on the item they are considering:
"There are 3 person(s) also watching this item"
and probably

"This item is just added to cart. That is the last one. You don't want to miss it."

and probably if the item is purchased / payment success by one person, the other person should be able to see message "The last item has just been sold. We're sorry...."

and also come up with some suggestion:
- View similar items

...

I meant, the mechanism for locking after making payment is already discussed in other comments. Here I just suggest a good notification and recommendation to enhance customer experience.

Any thoughts?

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

How about if we do something like booking.com?

There will be some kind of pushing notification to all the clients for every actions/events taken on the item they are considering:
"There are 3 person(s) also watching this item"
and probably

"This item is just added to cart. That is the last one. You don't want to miss it."

and probably if the item is purchased / payment success by one person, the other person should be able to see message "The last item has just been sold. We're sorry...."

and also come up with some suggestion:
- View similar items
...

I meant, the mechanism for locking after making payment is already discussed in other comments. Here I just suggest a good notification and recommendation to enhance customer experience.

Any thoughts?

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.

### 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.