TikTok Round 1 Backend Engineer New Grad
Just one question and then build up on that question.
Q- Basically create an inventory of some items with their count and then while checkout if the users cart items are available in inventory, it should return true and reflect in inventory, otherwise false
Soln -- used python dictionary with item as key and count as value . wrote a function checkout that took in input of the cart and checked the inventory and if all items were present in first iteration, reduce count on second iteration.
Follow up question (interview said this above is all coreect logic):
Q what could be a problem here. When and how will this fail ?
Soln - Told that in a multi thread env, when different people call the function, each user will have diff picture of inventory , so after checkout , our results could be wrong
Q- (interview was impressed) said how can we improve this ?
Soln - - Take lock on the inventory .
Q - Gave me rough code of mutex and said how to use it in this case .
Soln -- I put this in beginning of function and right before return statements.
Q-- with above implementation, what could be a problem?
Soln -- ( couldn't figure out , so he gave a hint and then i could figure out) Basically for each user dont take lock on entire inventory, but only on the items you are currently iterating on.