Magicsheet logo

Display Table of Food Orders in a Restaurant

Medium
70.4%
Updated 6/1/2025

Display Table of Food Orders in a Restaurant

What is this problem about?

The Display Table of Food Orders in a Restaurant coding problem asks you to generate a formatted table from a list of customer orders. Each order contains a customer name, a table number, and a food item. You need to create a summary where each row represents a table and each column represents a specific food item. The cells should contain the count of each food item ordered at that table. The table should be sorted by table number (numerically) and food items (alphabetically).

Why is this asked in interviews?

J.P. Morgan and other firms use this problem to test a candidate's ability to handle multi-dimensional data and sorting. It evaluations your mastery of Hash Table design patterns and your attention to detail regarding data types (e.g., table numbers as strings vs. integers). This is a practical, "real-world" problem that mirrors tasks like generating reports or pivot tables in a business application. It tests whether you can coordinate multiple maps and sets to build a structured output from flat data.

Algorithmic pattern used

This problem relies on Nested Hash Maps and Sorted Sets.

  1. Use a TreeSet or a sorted list to collect all unique food items alphabetically.
  2. Use a Map<Integer, Map<String, Integer>> to store the count of each food item per table. The outer key is the table number, and the inner key is the food item name.
  3. Extract the table numbers, sort them numerically, and iterate through them to build each row of the final table using the sorted food items as column headers.

Example explanation

Orders: [["Alice", "3", "Pizza"], ["Bob", "3", "Burger"], ["Charlie", "1", "Pizza"]]

  1. Unique Foods: ["Burger", "Pizza"] (Sorted).
  2. Mapping:
    • Table 1: {"Pizza": 1}
    • Table 3: {"Pizza": 1, "Burger": 1}
  3. Table Header: ["Table", "Burger", "Pizza"]
  4. Rows:
    • Table 1: ["1", "0", "1"]
    • Table 3: ["3", "1", "1"]

Common mistakes candidates make

  • Sorting Table Numbers: Treating table numbers as strings during sorting (e.g., "10" coming before "2") instead of converting them to integers.
  • Inefficient Lookups: Not using a nested map, which makes counting items for a specific table much slower.
  • Header Management: Forgetting to include the "Table" column in the header or the row.

Interview preparation tip

Practice using Map.getOrDefault() or similar constructs to simplify counter logic. For problems requiring two dimensions of sorting (rows and columns), always start by identifying the unique values for both dimensions and sorting them before attempting to build the final result.

Similar Questions