Domain 1EasyCORE

Customer Placing the Largest Number of Orders

**Restated Problem:** We need to find the customer who has placed the largest number of orders from a given set of order data. The input consists of two tables: `Customers` and `Orders`. The `Customers` table contains customer IDs and names, while the `Orders` table contains order IDs and associated customer IDs. The output should be the name of the customer with the highest number of orders.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 1

586. Customer Placing the Largest Number of Orders

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 75.5%


Problem Breakdown

Restated Problem: We need to find the customer who has placed the largest number of orders from a given set of order data. The input consists of two tables: Customers and Orders. The Customers table contains customer IDs and names, while the Orders table contains order IDs and associated customer IDs. The output should be the name of the customer with the highest number of orders.

Inputs:

  • Customers table:
    • customer_id: Unique identifier for each customer.
    • name: Name of the customer.
  • Orders table:
    • order_id: Unique identifier for each order.
    • customer_id: Identifier linking an order to a customer.

Outputs:

  • The name of the customer who has placed the largest number of orders.

Implicit Constraints and Edge Cases:

  1. Empty Input: If either the Customers or Orders table is empty, there are no customers with orders.
  2. Multiple Customers with Same Number of Orders: If multiple customers have the same highest number of orders, return any one of them.
  3. Single Customer: If there’s only one customer in the Customers table, that customer should be returned regardless of whether they have placed any orders.

Intuition

Brute-Force Approach:

  1. Join the Customers and Orders tables on customer_id.
  2. Group by customer_id to count the number of orders for each customer.
  3. Sort the results in descending order based on the count of orders.
  4. Select the top result.

Why Brute-Force is Insufficient:

  • The brute-force approach involves a join operation followed by grouping and sorting, which can be computationally expensive, especially with large datasets.
  • It has a time complexity of O(n log n) due to the sorting step, where n is the number of orders.

Key Insight: Instead of sorting all customers by their order count, we can use a single pass through the data to find the customer with the highest order count. This reduces the time complexity to linear.

Approach

  1. Join Tables: Perform an inner join between Customers and Orders on customer_id.
  2. Group By Customer: Group the results by customer_id to aggregate orders per customer.
  3. Count Orders: Count the number of orders for each customer.
  4. Track Maximum Order Count: Use a variable to keep track of the maximum order count encountered and the corresponding customer name.
  5. Return Result: Return the name of the customer with the highest order count.

Data Structures:

  • Use a dictionary to map customer_id to their respective order counts.
  • Use variables to store the maximum order count and the associated customer name.

Complexity Analysis

Time Complexity: O(n), where n is the number of orders. We perform a single pass through the data to count orders and determine the maximum.

Space Complexity: O(m), where m is the number of unique customers. We store the order counts for each customer in a dictionary.

Solution

import pandas as pd

def largest_orders(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
    # Join the Customers and Orders tables on customer_id
    merged_df = pd.merge(customers, orders, on='customer_id', how='left')
    
    # Group by customer_id and count the number of orders for each customer
    order_counts = merged_df.groupby('customer_id')['order_id'].count().reset_index()
    
    # Find the maximum order count
    max_orders = order_counts['order_id'].max()
    
    # Get the name of the customer(s) with the maximum order count
    result = customers[customers['customer_id'].isin(order_counts[order_counts['order_id'] == max_orders]['customer_id'])]
    
    # Return only the 'name' column
    return result[['name']]

Edge Cases Handled

  • Empty Orders Table: If there are no orders, the order_counts DataFrame will be empty, and max_orders will be 0. The resulting result DataFrame will also be empty, which is handled correctly by returning an empty DataFrame.
  • Multiple Customers with Same Maximum Order Count: The code uses isin to filter customers with the maximum order count, ensuring that all such customers are included in the result.
  • Single Customer: If there’s only one customer, the code will correctly identify them as having the highest number of orders (which could be zero) and return their name.