As reported: Toast Principal Software Engineer loop (2025), round 3: implement
getPrice(Collection<Menu> menus, MenuItem item)in Java over this model —Menu { name, groups, price },MenuGroup { name, groups, items, price },MenuItem { name, price }, every price nullable. Prices are inherited: an item with no price takes the price of its closest ancestor group or menu that has one. The sample data below and a lookup ofWingswere given. Also reported by a Toast candidate in 2025 as "given a menu of items, write the logic to fetch a specific item".
A restaurant has several menus. A menu has a name, an optional price and a list of groups. A group has a name, an optional price, sub-groups and items. An item has a name and an optional price.
getPrice(menus, item) finds the item with the same name and returns its price:
- the item's own price, if set;
- otherwise the price of its nearest ancestor (group, parent group, …, menu) that has a price;
nullif the item does not exist or no ancestor has a price.
Item names are unique across all menus.
Example (the sample data)
Lunch (11.50)
Appetizers (5.00): Wings (–), Fries (3.50)
Entrees (–): Burger (–)
Salads (7.50): Garden Salad (–), Cobb Salad w/ Bacon (9.00)
Toast (6.50): Avocado (–), PB&J (–)
Drinks (5.00)
Coffees (–)
Iced (5.00): Iced Latte (6.50), Decaf Iced Coffee (–)
Hot (3.00): Americano (–), Cup of Joe (–)
Sodas (2.50): Coke (–), Sprite (–), Ginger Ale (–)
getPrice(Wings) -> 5.00 (Appetizers)
getPrice(Burger) -> 11.50 (Entrees has no price -> the Lunch menu)
getPrice(Cobb Salad w/ Bacon) -> 9.00 (its own price)
getPrice(Cup of Joe) -> 3.00
getPrice(Pizza) -> null
Code runner format: line 1 is the menus as nested arrays — a menu is [name, price, groups], a group [name, price, groups, items], an item [name, price], with null for a missing price; line 2 is a list of item names to look up. The runner builds the Menu / MenuGroup / MenuItem objects, calls getPrice for each name, and prints the price with two decimals or null. (C++ uses -1 for "no price" and for "not found".)