Display Table of Food Orders in a Restaurant - Problem

Given an array orders representing customer orders in a restaurant, where each order is in the format [customerName, tableNumber, foodItem].

Return the restaurant's "display table" showing how many of each food item each table ordered. The display table should have:

  • First column: "Table" (table numbers)
  • Remaining columns: Food items in alphabetical order
  • Rows sorted by table number in numerically increasing order
  • Each cell shows the count of that food item for that table

Note: Customer names are not included in the output table.

Input & Output

Example 1 — Basic Restaurant Orders
$ Input: orders = [["David","3","Pizza"],["Corina","5","Burger"],["David","3","Pizza"],["Amos","5","Donut"]]
Output: [["Table","Burger","Donut","Pizza"],["3","0","0","2"],["5","1","1","0"]]
💡 Note: Table 3 ordered 2 Pizzas, Table 5 ordered 1 Burger and 1 Donut. Foods are sorted alphabetically: Burger, Donut, Pizza. Tables are sorted numerically: 3, 5.
Example 2 — Single Table Multiple Items
$ Input: orders = [["Alice","2","Salad"],["Bob","2","Salad"],["Charlie","2","Pasta"]]
Output: [["Table","Pasta","Salad"],["2","1","2"]]
💡 Note: Only table 2 with orders: 1 Pasta and 2 Salads. Foods sorted alphabetically: Pasta comes before Salad.
Example 3 — Multiple Tables Single Items
$ Input: orders = [["John","1","Coffee"],["Jane","2","Coffee"],["Jim","3","Coffee"]]
Output: [["Table","Coffee"],["1","1"],["2","1"],["3","1"]]
💡 Note: Three tables each ordered 1 Coffee. Tables sorted numerically: 1, 2, 3.

Constraints

  • 1 ≤ orders.length ≤ 5 × 104
  • orders[i].length == 3
  • 1 ≤ customerNamei.length, foodItemi.length ≤ 20
  • customerNamei and foodItemi consist of lowercase and uppercase English letters and spaces
  • tableNumberi is a valid integer

Visualization

Tap to expand
Display Table of Food Orders INPUT orders array: ["David", "3", "Pizza"] ["Corina", "5", "Burger"] ["David", "3", "Pizza"] ["Amos", "5", "Donut"] [customerName, tableNumber, foodItem] Table 3 Table 5 Tables: 3, 5 Foods: Burger, Donut, Pizza ALGORITHM STEPS 1 Collect unique foods foodSet: {Burger, Donut, Pizza} 2 Collect unique tables tableSet: {3, 5} 3 Build count map map[table][food] = count map["3"]["Pizza"] = 2 map["5"]["Burger"] = 1 map["5"]["Donut"] = 1 // others = 0 4 Build result table Sort foods alphabetically Sort tables numerically FINAL RESULT Display Table: Table Burger Donut Pizza 3 0 0 2 5 1 1 0 Output Array: [["Table","Burger", "Donut","Pizza"], ["3","0","0","2"], ["5","1","1","0"]] OK - Complete! 2 tables, 3 food items Key Insight: Use a nested HashMap (map[tableNumber][foodItem] = count) to efficiently track order counts. Single pass through orders: O(n) time. Collect unique tables and foods using Sets for easy sorting. Final step: Sort foods alphabetically and tables numerically to build the display table. TutorialsPoint - Display Table of Food Orders in a Restaurant | Hash Map - Single Pass Counting
Asked in
Amazon 15 Google 8 Microsoft 6
18.0K Views
Medium Frequency
~25 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen