Drop Type 1 Orders for Customers With Type 0 Orders - Problem

Drop Type 1 Orders for Customers With Type 0 Orders

You are working with an e-commerce database that tracks customer orders. Each order has a unique ID, belongs to a customer, and has a specific order type (either 0 or 1).

Your task is to implement a priority filtering system where:

  • Type 0 orders have higher priority than Type 1 orders
  • If a customer has at least one Type 0 order, we should exclude all their Type 1 orders from the results
  • If a customer has only Type 1 orders, we include all their orders

Think of this as a VIP customer system where Type 0 represents premium orders and Type 1 represents regular orders. Once a customer places a premium order, we only show their premium activity.

Goal: Filter the orders table based on the priority rules above and return the filtered results.

Input & Output

example_1.py โ€” Python
$ Input: orders = [[1,1,0],[2,1,1],[3,2,1],[4,3,0],[5,3,1]]
โ€บ Output: [[1,1,0],[3,2,1],[4,3,0]]
๐Ÿ’ก Note: Customer 1 has both Type 0 (order 1) and Type 1 (order 2) orders, so we only include the Type 0 order. Customer 2 has only Type 1 orders, so we include all (order 3). Customer 3 has both types, so we only include Type 0 (order 4).
example_2.py โ€” Python
$ Input: orders = [[1,1,1],[2,1,1],[3,2,1]]
โ€บ Output: [[1,1,1],[2,1,1],[3,2,1]]
๐Ÿ’ก Note: All customers (1 and 2) only have Type 1 orders, so we include all orders in the result.
example_3.py โ€” Python
$ Input: orders = [[1,1,0],[2,2,0],[3,3,0]]
โ€บ Output: [[1,1,0],[2,2,0],[3,3,0]]
๐Ÿ’ก Note: All customers only have Type 0 orders, so we include all orders. Since they don't have Type 1 orders, there's nothing to filter out.

Constraints

  • 1 โ‰ค orders.length โ‰ค 104
  • orders[i].length == 3
  • 1 โ‰ค order_id, customer_id โ‰ค 1000
  • order_type is either 0 or 1
  • order_id is unique for each order

Visualization

Tap to expand
๐Ÿฝ๏ธ VIP Restaurant Reservation System๐Ÿ“‹ All ReservationsVIPCustomer AREGCustomer AREGCustomer BProcess๐ŸŽฏ Customer Analysis๐Ÿ‘‘ Customer A: Has VIP๐Ÿ‘ค Customer B: Regular onlyApply priority rules...Filterโœจ Final ResultVIPCustomer AREGCustomer BOnly VIP for A, All for B๐Ÿ’ก Key InsightOnce a customer achieves VIP status (Type 0 order),only their VIP reservations appear in the premium system.Regular customers see all their reservations.๐ŸŽฏ Optimal Solution: O(n) time with single-pass grouping
Understanding the Visualization
1
Scan Reservations
Process each reservation and categorize customers by their status
2
Identify VIP Status
Keep track of which customers have made VIP reservations (Type 0)
3
Apply Priority Rules
For VIP customers, show only VIP reservations. For regular customers, show all reservations
Key Takeaway
๐ŸŽฏ Key Insight: Use hash table grouping to efficiently categorize customers and apply priority rules in a single pass through the data.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
42.0K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen