LeetCode Problem Workspace

Display Table of Food Orders in a Restaurant

Generate a restaurant display table from orders by counting each food item per table using array scanning and hash lookup.

category

5

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Medium · Array scanning plus hash lookup

bolt

Answer-first summary

Generate a restaurant display table from orders by counting each food item per table using array scanning and hash lookup.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array scanning plus hash lookup

Try AiBox Copilotarrow_forward

This problem requires transforming an array of customer orders into a structured display table showing counts of each food item per table. The approach leverages array scanning with hash table lookups to tally orders efficiently while sorting tables numerically and food items alphabetically. Implementing this correctly avoids miscounting and ensures the output matches the required header and row order.

Problem Statement

Given a list of orders where each order is represented as [customerName, tableNumber, foodItem], create a display table that shows how many of each food item was ordered per table. Each row corresponds to a table and counts each food item ordered at that table. The first column is the table number, and subsequent columns are food items sorted alphabetically.

Return the display table as a 2D array of strings with the first row as the header starting with 'Table', followed by sorted food items. Rows should be sorted by table number in ascending order. Customer names are ignored, and each cell contains the count of orders for that food item at the corresponding table.

Examples

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"]]

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"]]

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"]]

Example details omitted.

Constraints

  • 1 <= orders.length <= 5 * 10^4
  • orders[i].length == 3
  • 1 <= customerNamei.length, foodItemi.length <= 20
  • customerNamei and foodItemi consist of lowercase and uppercase English letters and the space character.
  • tableNumberi is a valid integer between 1 and 500.

Solution Approach

Collect Unique Food Items

Scan all orders and add each food item to a set to determine the complete list of items. Then sort this list alphabetically to form the display table header after 'Table'.

Map Table Orders

Use a hash table to map each table number to a secondary hash counting the frequency of each food item. Iterate through all orders, updating the count of the corresponding food item for each table.

Build and Sort the Table

Create the final display table starting with the header row. Sort the table numbers numerically and construct each row by fetching counts from the hash table in the order of the sorted food items.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n + m log m + k log k) where n is number of orders, m is number of unique tables, and k is number of unique food items due to scanning, sorting tables, and sorting food items. Space complexity is O(m*k) to store the count mapping of tables to food items.

What Interviewers Usually Probe

  • Candidate correctly identifies array scanning combined with hash map counting.
  • Candidate properly sorts tables numerically and food items alphabetically.
  • Candidate handles edge cases like tables with missing orders for certain food items.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to sort the food items alphabetically in the header.
  • Mixing up table numbers as strings versus integers when sorting.
  • Incorrectly counting multiple orders of the same food item for a table.

Follow-up variants

  • Return a display table showing only the top N most ordered food items.
  • Include customer names in a secondary structure alongside food counts per table.
  • Handle streaming orders where counts need to update dynamically.

FAQ

What pattern does 'Display Table of Food Orders in a Restaurant' follow?

It follows an array scanning plus hash lookup pattern where each table-food pair is counted using a hash map.

How do I ensure the header row is correct?

Collect all unique food items from orders and sort them alphabetically, then prepend 'Table' to form the header.

Should table numbers be treated as strings or integers?

Sort table numbers numerically as integers but convert to strings when constructing the final display table.

How to handle multiple orders of the same food at the same table?

Increment the count in the hash map for that table-food pair each time the item appears.

What are the main failure modes for this problem?

Failing to sort food items alphabetically, miscounting duplicate orders, and not sorting tables numerically are common mistakes.

terminal

Solution

Solution 1: Hash Table + Sorting

We can use a hash table $\textit{tables}$ to store the dishes ordered at each table, and a set $\textit{items}$ to store all the dishes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tables = defaultdict(list)
        items = set()
        for _, table, foodItem in orders:
            tables[int(table)].append(foodItem)
            items.add(foodItem)
        sorted_items = sorted(items)
        ans = [["Table"] + sorted_items]
        for table in sorted(tables):
            cnt = Counter(tables[table])
            row = [str(table)] + [str(cnt[item]) for item in sorted_items]
            ans.append(row)
        return ans
Display Table of Food Orders in a Restaurant Solution: Array scanning plus hash lookup | LeetCode #1418 Medium