FastPrepCustomer Order Count Distribution by Country

Customer Order Count Distribution by Country

Apple logoApple● EasyFULLTIMEPHONE SCREEN

Problem statement

For every country, count its customers in three mutually exclusive order-frequency categories:

  • zero_order_customers: customers who have never placed an order.
  • one_order_customers: customers who have placed exactly one order.
  • multiple_order_customers: customers who have placed at least two orders.

Include every country, even when it has no customers. Customers with no matching row in orders must remain in the result.

Result

Return country_id, country_name, zero_order_customers, one_order_customers, and multiple_order_customers. Order rows by country_name ascending.

Table schema

MySQLPostgreSQLPandas

Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.

countries

ColumnTypeNullableDescription
country_idPKIntegerNo—
country_nameTextNo—

customers

ColumnTypeNullableDescription
customer_idPKIntegerNo—
country_idIntegerNo—

Foreign key: country_id → countries(country_id)

orders

ColumnTypeNullableDescription
order_idPKIntegerNo—
customer_idIntegerNo—
country_idIntegerNo—
order_dateDateNo—

Foreign key: customer_id → customers(customer_id)

Foreign key: country_id → countries(country_id)

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
country_idIntegerNo—
country_nameTextNo—
zero_order_customersIntegerNo—
one_order_customersIntegerNo—
multiple_order_customersIntegerNo—

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • Each customer belongs to exactly one country.
  • Every order belongs to exactly one customer, and its country_id matches that customer's country.
  • Every country_name is unique.
  • A customer's category is based on the number of distinct order rows for that customer.
  • Each input table contains at most 2,000 rows.

More Apple problems

See Apple hiring insights