Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1418: Display Table of Food Orders in a Restaurant

In this guide, we solve Leetcode #1418 Display Table of Food Orders in a Restaurant in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Array, Hash Table, String, Ordered Set, Sorting

Intuition

Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.

By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.

Approach

Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.

This keeps the solution linear while remaining easy to explain in an interview setting.

Steps:

  • Initialize a hash map for seen items or counts.
  • Iterate through the input, querying/updating the map.
  • Return the first valid result or the final computed value.

Example

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

Python Solution

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

Complexity

The time complexity is O(n+m×log⁡m+k×log⁡k+m×k)O(n + m \times \log m + k \times \log k + m \times k)O(n+m×logm+k×logk+m×k), and the space complexity is O(n+m+k)O(n + m + k)O(n+m+k). The space complexity is O(n+m+k)O(n + m + k)O(n+m+k).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy