FastPrepiPhone Model Sales Share by Country

iPhone Model Sales Share by Country

Apple logoApple● MediumFULLTIMEPHONE SCREEN

Problem statement

Apple tracks iPhone orders by country. For every country and every model that names a previous-generation model, compare the two models' shares of that country's total iPhone units sold.

Return one row when either the current model or its previous model sold at least one unit in the country. A missing side of the comparison has zero units.

Result

Return country_name, product_name, current_share_percent, previous_share_percent, and share_difference_percentage_points. A model share is its units divided by all units sold in the same country, multiplied by 100 and rounded to two decimal places. The difference is current share minus previous share.

Order rows by country_name ascending, then product_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.

products

ColumnTypeNullableDescription
product_idPKIntegerNo—
product_nameTextNo—
previous_model_idIntegerYes—

Foreign key: previous_model_id → products(product_id)

countries

ColumnTypeNullableDescription
country_idPKIntegerNo—
country_nameTextNo—

orders

ColumnTypeNullableDescription
order_idPKIntegerNo—
customer_idIntegerNo—
country_idIntegerNo—
order_dateDateNo—

Foreign key: country_id → countries(country_id)

order_items

ColumnTypeNullableDescription
order_idPKIntegerNo—
product_idPKIntegerNo—
quantityIntegerNo—
unit_priceDecimalNo—

Foreign key: order_id → orders(order_id)

Foreign key: product_id → products(product_id)

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
country_nameTextNo—
product_nameTextNo—
current_share_percentDecimalNo—
previous_share_percentDecimalNo—
share_difference_percentage_pointsDecimalNo—

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

Constraints

  • Every quantity is a positive integer.
  • Every row in products represents one iPhone model, and product_name values are unique.
  • Every previous_model_id is either NULL or identifies an older product.
  • Every country_name is unique.
  • Each input table contains at most 2,000 rows.

More Apple problems

See Apple hiring insights