KatInterview

Join User Profiles and Purchase Records, Fill Gaps, Compute Line Spend

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datadata-manipulationjoinsnull-handling

Last updated

Tests left-join logic, mode imputation for missing categorical fields, and null-safe row-level spend calculation using plain Python dicts.

Join User Profiles and Purchase Records, Fill Gaps, Compute Line Spend

You receive two lists of dictionaries representing a members table and a purchases table.

members — one entry per member:

FieldTypeNotes
idintunique member identifier
namestrdisplay name
countrystr or Nonehome country; may be missing

purchases — one entry per purchase:

FieldTypeNotes
idintunique purchase identifier
customer_idintforeign key into members
amountfloat or Nonepurchase value; may be missing

Task

Return a flat list of dictionaries, one row per purchase, keeping every member even if they made no purchases. Each output row must contain exactly these keys:

KeyValue
idthe member's id
namethe member's name
countrythe member's country after imputation (see below)
order_idthe purchase's id, or None if the member has no purchases
amountthe raw purchase amount (unchanged, so possibly None), or None if no purchases
total_spent0 if amount is None or the member has no purchases; otherwise equal to amount

Filling missing countries

Before building output rows, determine an imputed country to substitute for any None country value:

  1. Collect all non-None country values from the members list only (not from the joined result).
  2. Find the mode — the country appearing most often.
  3. Break ties by choosing the country that comes first alphabetically.
  4. If every member's country is None, do not replace any values; leave them as None.

Apply the imputed country to every member whose country is None.

Sorting

Return the final list sorted by total_spent descending. The relative order among rows with equal total_spent is not specified.

Constraints

  • 1 ≤ len(members) ≤ 10 000
  • 0 ≤ len(purchases) ≤ 50 000
  • Every purchase.customer_id appears in members.
  • amount, when present, is a non-negative float.
  • A member may have zero, one, or many purchases.
  • id values within each table are unique.

Examples

Example 1

Input: merge_and_clean_customer_orders([{'id': 1, 'name': 'Alice', 'country': 'US'}, {'id': 2, 'name': 'Bob', 'country': 'UK'}, {'id': 3, 'name': 'Charlie', 'country': None}], [{'id': 101, 'customer_id': 1, 'amount': 250.5}, {'id': 102, 'customer_id': 2, 'amount': 99.99}, {'id': 103, 'customer_id': 2, 'amount': None}])

Output: [{'id': 1, 'name': 'Alice', 'country': 'US', 'order_id': 101, 'amount': 250.5, 'total_spent': 250.5}, {'id': 2, 'name': 'Bob', 'country': 'UK', 'order_id': 102, 'amount': 99.99, 'total_spent': 99.99}, {'id': 2, 'name': 'Bob', 'country': 'UK', 'order_id': 103, 'amount': None, 'total_spent': 0}, {'id': 3, 'name': 'Charlie', 'country': 'UK', 'order_id': None, 'amount': None, 'total_spent': 0}]

Hints

Hint 1

Index purchases by customer_id into a dict before the main loop so each member's purchases are found in O(1) instead of scanning the full list each time.

Hint 2

To find the mode with an alphabetical tie-break, count frequencies with a plain dict, then select the winner with min(counts, key=lambda c: (-counts[c], c)) — negative frequency puts the most-common country first, and the string itself resolves ties alphabetically.

Hint 3

A member with no purchases should still appear exactly once in the output, with order_id=None and amount=None. Don't confuse amount (left raw as None) with total_spent (always a number, defaulting to 0).