We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Display Table of Food Orders in a Restaurant

Number: 1533

Difficulty: Medium

Paid? No

Companies: Nordstrom, J.P. Morgan


Problem Description

Given a list of orders where each order is represented as [customerName, tableNumber, foodItem], construct and return a "display table" of the restaurant. The display table is a 2D table where the first row is a header row with "Table" as the first column followed by the list of food items (in alphabetical order). Each subsequent row corresponds to a table (sorted numerically) and shows the counts of each food item ordered at that table.


Key Insights

  • Use a set to gather all unique food items, then sort them alphabetically.
  • Use a hashmap/dictionary to map table numbers to their counts of food items.
  • Since table numbers are given as strings, sort them after converting to integers.
  • Build the final display table with the header row and then each row corresponding to a specific table.

Space and Time Complexity

Time Complexity: O(N log N + F log F) where N is the number of orders and F is the number of unique food items. Space Complexity: O(N + F), to store the orders mapped by table and the unique food items.


Solution

We iterate over each order, storing the food items in a set to deduce the header and updating a mapping for each table with the counts of food items. After processing, we sort the food items alphabetically and the table numbers numerically. Finally, we build the display table by first adding the header row, then for each table, add a row containing the table number followed by counts (defaults to zero if not ordered) for each food item.


Code Solutions

def displayTable(orders):
    # Initialize a set for unique food items and a dictionary for table orders.
    food_items = set()
    table_orders = {}
    
    # Process each order.
    for order in orders:
        customer, table, food = order
        food_items.add(food)
        if table not in table_orders:
            table_orders[table] = {}
        # Increase the count for the food item at the table.
        table_orders[table][food] = table_orders[table].get(food, 0) + 1
    
    # Sort the food items alphabetically.
    sorted_food = sorted(food_items)
    
    # Build the result starting with the header row.
    result = []
    header = ["Table"] + sorted_food
    result.append(header)
    
    # Sort the tables numerically.
    for table in sorted(table_orders.keys(), key=lambda x: int(x)):
        row = [table]
        # For each food item, append the count (as a string) or 0 if not ordered.
        for food in sorted_food:
            row.append(str(table_orders[table].get(food, 0)))
        result.append(row)
    return result

# Example usage:
orders = [["David","3","Ceviche"],
          ["Corina","10","Beef Burrito"],
          ["David","3","Fried Chicken"],
          ["Carla","5","Water"],
          ["Carla","5","Ceviche"],
          ["Rous","3","Ceviche"]]
print(displayTable(orders))
← Back to All Questions