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
Restaurant Order Display Table ProblemInput: Orders["David", "3", "Pizza"]["Corina", "5", "Burger"]["David", "3", "Pizza"]["Amos", "5", "Donut"]Customer orders by tableProcessCount by table:Table 3: Pizza × 2Table 5: Burger × 1Table 5: Donut × 1Group & count itemsOutput: Display TableTable | Burger | Donut | Pizza 3 | 0 | 0 | 2 5 | 1 | 1 | 0✓ Tables sorted numerically✓ Foods sorted alphabeticallyTransform customer orders into organized table formatKey: Count occurrences efficiently, then sort and format
Understanding the Visualization
1
Input Orders
Array of [customer, table, food] orders from restaurant
2
Count & Group
Group orders by table and food item, count occurrences
3
Format Table
Create sorted display table with table numbers and food counts
Key Takeaway
🎯 Key Insight: Use hash maps to count table-food combinations efficiently, then sort both dimensions for the final display table
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