Amazon Interview Question
Country: United States
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.
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)
Thanks bharat for solution, sorry for question change. Previous question has already been answered.
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.
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.
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.
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.
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.
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.
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
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.
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.
How about this:
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.
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.
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.
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:
- Add it to your wishlist and receive email whenever it's available back
- 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?
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:
- Add it to your wishlist and receive email whenever it's available back
- 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?
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?
- Coder... April 22, 2015