Display Table of Food Orders in a Restaurant - Problem

You're building a restaurant management system that needs to display order summaries. Given an array of customer orders, create a display table that shows how many of each food item was ordered at each table.

Input Format: Each order is represented as [customerName, tableNumber, foodItem]

Output Format: A 2D table where:

  • First row is the header: ["Table", "food1", "food2", ...] (food items in alphabetical order)
  • Each subsequent row: ["tableNum", count1, count2, ...] (tables in numerical order)
  • Customer names are not included in the final table

Example: If orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"]], the output shows table 12 ordered 3 Fried Chicken items.

Input & Output

example_1.py — Basic Restaurant Orders
$ Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]
💡 Note: Table 3 has 2 Ceviche and 1 Fried Chicken. Table 5 has 1 Ceviche and 1 Water. Table 10 has 1 Beef Burrito. Foods are sorted alphabetically, tables numerically.
example_2.py — Single Table Multiple Items
$ Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]]
💡 Note: Table 1 ordered 2 Canadian Waffles, Table 12 ordered 3 Fried Chicken. Customer names don't appear in final table.
example_3.py — Edge Case: Single Order
$ Input: orders = [["Laura","2","Bean Burrito"]]
Output: [["Table","Bean Burrito"],["2","1"]]
💡 Note: Minimal case: one customer, one table, one food item results in a 2x2 table with header and single data row.

Constraints

  • 1 ≤ orders.length ≤ 5 × 104
  • orders[i].length == 3
  • 1 ≤ customerNameᵢ.length, foodItemᵢ.length ≤ 20
  • customerNameᵢ and foodItemᵢ consist of lowercase and uppercase English letters and the space character
  • tableNumberᵢ is a valid integer between 1 and 500

Visualization

Tap to expand
🍽️ Restaurant Order Management📋 Order SlipsJames, Table 12, PizzaAnna, Table 5, SaladBob, Table 12, PizzaCarl, Table 5, BurgerDave, Table 3, SaladProcess🔄 Hash MapTable 3Salad: 1Table 5Salad: 1Burger: 1Table 12Pizza: 2Format📊 Display TableTable|Burger|Pizza|Salad 3 | 0 | 0 | 1 5 | 1 | 0 | 1 12 | 0 | 2 | 0 ✓ Foods alphabetically sorted✓ Tables numerically sorted🎯 Algorithm Steps1Single Pass: Build nested hash map table→food→count2Sort Collections: Extract foods (alphabetical) and tables (numerical)3Build Table: Use hash map for O(1) count lookupsTime: O(n + f log f + t log t) - linear in orders, log in unique items
Understanding the Visualization
1
Collect Order Slips
Each slip has customer name, table number, and food item
2
Group by Table
Sort slips by table number, counting food items per table
3
Create Summary Board
Display table shows quantities needed per table (no customer names)
4
Organize Layout
Foods in alphabetical columns, tables in numerical rows for easy reading
Key Takeaway
🎯 Key Insight: Nested hash maps (table → food → count) enable single-pass processing with O(1) lookups, making this optimal for restaurant order management where quick aggregation is essential.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
23.5K Views
Medium Frequency
~15 min Avg. Time
892 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