Watch 6 video solutions for Display Table of Food Orders in a Restaurant, a medium level problem involving Array, Hash Table, String. This walkthrough by Leetcode and Interview Question has 1,164 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders.
Return the restaurant's “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.
Example 1:
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"]] Explanation: The displaying table looks like: Table,Beef Burrito,Ceviche,Fried Chicken,Water 3 ,0 ,2 ,1 ,0 5 ,0 ,1 ,0 ,1 10 ,1 ,0 ,0 ,0 For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche". For the table 5: Carla orders "Water" and "Ceviche". For the table 10: Corina orders "Beef Burrito".
Example 2:
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"]] Explanation: For the table 1: Adam and Brianna order "Canadian Waffles". For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
Example 3:
Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]] Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]
Constraints:
1 <= orders.length <= 5 * 10^4orders[i].length == 31 <= customerNamei.length, foodItemi.length <= 20customerNamei and foodItemi consist of lowercase and uppercase English letters and the space character.tableNumberi is a valid integer between 1 and 500.Problem Overview: You receive a list of restaurant orders where each record contains a customer name, table number, and food item. The goal is to generate a display table showing how many of each food item every table ordered. Food names must appear in alphabetical order and table numbers must appear in numeric order.
Approach 1: Using Dictionary for Table and Food Mapping (O(n log f + t log t) time, O(t * f) space)
The core idea is to aggregate counts using a nested hash map. Iterate through the orders and maintain a mapping like table -> {food -> count}. At the same time, track all unique food items using a set so the header row can be sorted alphabetically. After processing all orders, sort the food names and table numbers. Then construct the final matrix row by row by reading counts from the hash map, defaulting to 0 when a table never ordered a specific food. Hash lookups make updates constant time, which keeps the counting step efficient. This approach relies heavily on Hash Table lookups and simple Array construction.
Approach 2: Using 2D Array and Index Mapping (O(n + f log f + t log t) time, O(t * f) space)
This version precomputes indexes for both foods and tables and stores counts directly inside a 2D grid. First collect all unique foods and sort them alphabetically. Assign each food a column index using a dictionary. Do the same for tables (sorted numerically). Then allocate a 2D array where rows represent tables and columns represent foods. Iterate through the orders and increment the corresponding cell using the precomputed indexes. Finally prepend the header row and convert counts to strings for the output. This approach avoids nested maps and instead relies on direct index access, which can be slightly faster in languages like C++ or JavaScript. Sorting still uses standard Sorting operations to maintain the required output order.
Recommended for interviews: The dictionary mapping approach is the most common explanation because it mirrors the natural grouping of table -> food counts. It clearly demonstrates proficiency with hash tables and aggregation problems. The 2D array version is equally valid and sometimes preferred when you want tighter control over memory layout or faster indexed updates.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dictionary for Table and Food Mapping | O(n log f + t log t) | O(t * f) | General solution; easiest to implement using hash maps and sets |
| 2D Array with Index Mapping | O(n + f log f + t log t) | O(t * f) | When you want direct indexed updates instead of nested maps |