You have to implement functionalities like add restaurant, update menu, place order, dispatch order etc as a set of commands.
Orders are placed to restaurants based on a restaurant selection strategy.
"itemName:price" (example: "burger:120")."burger,pizza"). List<String> processCommands(List<String> commands)
commands.size() >= 0timestamp.timestamp order.commands). List<String> getRestaurantItemCounts()
"restaurantId|itemName|count" List<String> getDispatchedOrders(String restaurantId)
"dispatchTimestamp|orderId|customerId|items"items is a comma-separated list (example: "burger,pizza").|.
timestamp|ADD_RESTAURANT|restaurantId|capacity|menu
capacity is the maximum number of concurrent open (not yet dispatched) orders the restaurant can hold.menu is a comma-separated list of "item:price" entries.101|ADD_RESTAURANT|R1|2|burger:120,pizza:200OK if added, else RESTAURANT_ALREADY_EXISTS.timestamp|UPDATE_MENU|restaurantId|menuUpdates
menuUpdates is a comma-separated list of "item:price" entries.price >= 0, upsert (add or update) the item price.price < 0, remove the item from existing menu (if present).105|UPDATE_MENU|R1|burger:130,pasta:150,salad:-1OK if updated, else RESTAURANT_NOT_FOUND.timestamp|PLACE_ORDER|orderId|customerId|strategy|items
items is a comma-separated list of item names (example: "burger,pizza").openOrders < capacity). ACCEPTED, else REJECTED if no restaurant can be found or INVALID_STRATEGY if strategy doesn't exist.strategy:
LOWEST_TOTAL_PRICE: pick the restaurant with minimum total price of requested items. Tie-breakers (in order):
capacity - openOrders)restaurantIdMAX_REMAINING_CAPACITY: pick the restaurant with maximum remaining capacity. Tie-breakers:
restaurantIdINVALID_STRATEGY.timestamp|DISPATCH_ORDER|orderId
DISPATCHED, or INVALID_ORDER, or ALREADY_DISPATCHED.processCommands must return exactly one output string per command, in the same order as the input command list.0 <= timestamp <= 10^120 <= commands.size() <= 1000001 <= capacity <= 1000000 <= price <= 10^9 (negative price only allowed in UPDATE_MENU to remove an item)| or , characters. FoodCart foodCart = new FoodCart();
foodCart.processCommands(commands = List.of(
"200|PLACE_ORDER|O1|C1|LOWEST_TOTAL_PRICE|burger,pizza""100|ADD_RESTAURANT|R1|2|burger:120,pizza:200""150|ADD_RESTAURANT|R2|1|burger:110,pizza:220""210|PLACE_ORDER|O2|C2|LOWEST_TOTAL_PRICE|burger""220|DISPATCH_ORDER|O1""205|UPDATE_MENU|R2|pizza:180""230|PLACE_ORDER|O3|C3|LOWEST_TOTAL_PRICE|pizza")
100: add R1150: add R2200: place order O1 for C1 (burger,pizza) with LOWEST_TOTAL_PRICE
O1 → output ACCEPTED205: update R2 pizza price to 180210: place order O2 for C2 (burger) with LOWEST_TOTAL_PRICE
O2 → output ACCEPTED220: dispatch order O1
O1 dispatched, frees 1 slot in R1, and increments served counts for R1: burger +1, pizza +1DISPATCHED230: place order O3 for C3 (pizza) with LOWEST_TOTAL_PRICE
O3 → output ACCEPTED List.of(
"ACCEPTED""OK""OK""ACCEPTED""DISPATCHED""OK""ACCEPTED")
foodCart.getRestaurantItemCounts()
"R1|burger|1""R1|pizza|1"O1 was dispatched, and it belonged to restaurant R1 with items burger and pizza. Rows are already in lexicographic ascending order.
foodCart.getDispatchedOrders(restaurantId = "R1")
"220|O1|C1|burger,pizza"