Hone logo
Hone
Problems

Customer Placing the Largest Number of Orders

This challenge involves analyzing customer order data to identify the customer who has placed the most orders. This is a common task in e-commerce analytics, helping businesses understand their most active customers and tailor marketing strategies or loyalty programs.

Problem Description

You will be given a dataset of customer orders. Each record in the dataset represents a single order and includes at least a customer identifier and an order identifier. Your task is to determine which customer has placed the highest number of orders. If there's a tie for the highest number of orders, you should return all customers who share that maximum count.

Key Requirements:

  • Count the total number of orders placed by each unique customer.
  • Identify the maximum order count among all customers.
  • Return the identifier(s) of the customer(s) who placed this maximum number of orders.

Expected Behavior:

The output should be a list or collection of customer identifiers.

Edge Cases:

  • Empty Dataset: If the input dataset is empty, the output should reflect this (e.g., an empty list).
  • Single Customer: If there is only one customer in the dataset, that customer should be returned.
  • All Customers with Same Number of Orders: If all customers have placed the exact same number of orders, all customer identifiers should be returned.

Examples

Example 1:

Input:
orders = [
  {"customer_id": "C101", "order_id": "O001"},
  {"customer_id": "C102", "order_id": "O002"},
  {"customer_id": "C101", "order_id": "O003"},
  {"customer_id": "C103", "order_id": "O004"},
  {"customer_id": "C101", "order_id": "O005"},
  {"customer_id": "C102", "order_id": "O006"}
]
Output:
["C101"]

Explanation: Customer "C101" placed 3 orders. Customer "C102" placed 2 orders. Customer "C103" placed 1 order. The maximum number of orders is 3, placed by customer "C101".

Example 2:

Input:
orders = [
  {"customer_id": "A", "order_id": "1"},
  {"customer_id": "B", "order_id": "2"},
  {"customer_id": "A", "order_id": "3"},
  {"customer_id": "C", "order_id": "4"},
  {"customer_id": "B", "order_id": "5"},
  {"customer_id": "A", "order_id": "6"},
  {"customer_id": "C", "order_id": "7"}
]
Output:
["A"]

Explanation: Customer "A" placed 3 orders. Customer "B" placed 2 orders. Customer "C" placed 2 orders. The maximum number of orders is 3, placed by customer "A".

Example 3: (Tie Scenario)

Input:
orders = [
  {"customer_id": "X", "order_id": "10"},
  {"customer_id": "Y", "order_id": "11"},
  {"customer_id": "X", "order_id": "12"},
  {"customer_id": "Y", "order_id": "13"},
  {"customer_id": "Z", "order_id": "14"}
]
Output:
["X", "Y"]

Explanation: Customer "X" placed 2 orders. Customer "Y" placed 2 orders. Customer "Z" placed 1 order. The maximum number of orders is 2, shared by customers "X" and "Y".

Constraints

  • The input orders will be a collection (e.g., list, array) of order objects.
  • Each order object will contain at least a "customer_id" (string) and an "order_id" (string).
  • Customer IDs will be unique strings. Order IDs will be unique strings.
  • The number of orders in the input collection can range from 0 to 1,000,000.
  • The length of customer_id and order_id strings will be between 1 and 50 characters.

Notes

  • You can assume that the orders collection will be provided in a structured format suitable for iteration.
  • Consider using a data structure that efficiently maps customer IDs to their order counts.
  • The order of the customer identifiers in the output list does not matter.

Here's a pseudocode outline to guide your thinking:

FUNCTION find_top_customer(orders):
  IF orders IS EMPTY:
    RETURN EMPTY_LIST

  // Initialize a map to store customer order counts
  customer_order_counts = NEW_MAP()

  // Iterate through each order in the dataset
  FOR EACH order IN orders:
    customer_id = order.customer_id

    // Increment the count for the current customer
    IF customer_id IS IN customer_order_counts:
      customer_order_counts[customer_id] = customer_order_counts[customer_id] + 1
    ELSE:
      customer_order_counts[customer_id] = 1

  // Find the maximum order count
  max_orders = 0
  FOR EACH count IN VALUES(customer_order_counts):
    IF count > max_orders:
      max_orders = count

  // Collect all customers who have the maximum order count
  top_customers = NEW_LIST()
  FOR EACH customer_id, count IN customer_order_counts:
    IF count == max_orders:
      ADD customer_id TO top_customers

  RETURN top_customers
Loading editor...
plaintext