KatInterview

Deduplicate and Canonicalize Customer Records

SQL & DataAuto-graded · 15 test cases
MediumAuto-gradedSQL & Datadata cleaningdeduplicationdictionaries

Last updated

Tests ability to clean a list of raw customer record dicts by resolving duplicates, normalizing fields, and returning one canonical record per customer.

Deduplicate and Canonicalize Customer Records

You are given a list of raw customer records extracted from a database. Due to multiple data-entry sources, the same customer may appear several times with slightly inconsistent data. Your job is to clean this list and return exactly one canonical record per customer.

Record Structure

Each record is a dict with these keys:

KeyTypeNotes
customer_idstrMay have leading/trailing whitespace; case-insensitive (e.g. "C001" and " c001 " are the same customer)
namestrMay have extra whitespace; keep the longest stripped version across duplicates; tie-break: lexicographically largest
emailstrLowercase it; keep the most frequent email among duplicates; tie-break: lexicographically smallest
spendfloat or intKeep the maximum value across duplicates
activeboolTrue wins over False (i.e. if any duplicate is active, the canonical record is active)

Canonicalization Rules (applied per customer group)

  1. customer_id: Strip whitespace and uppercase (e.g. " c001 " → "C001").
  2. name: Strip each name variant, then pick the longest. On a length tie, pick the lexicographically largest string.
  3. email: Lowercase all variants, then pick the most frequent. On a frequency tie, pick the lexicographically smallest.
  4. spend: Maximum across all duplicates.
  5. active: True if any duplicate has True, otherwise False.

Output

Return a list of canonical records (one per unique customer_id), sorted ascending by canonical customer_id.

Each returned dict must have exactly the keys: customer_id, name, email, spend, active.

Constraints

  • 0 ≤ len(records) ≤ 2000
  • Every record contains all five keys.
  • customer_id after stripping is always a non-empty string.
  • spend values are finite numbers in the range [0, 1_000_000].
  • There is always at least one record per customer group (no empty groups).
  • The input list may already be fully deduplicated, or every record may be a duplicate.

Examples

Example 1

Input: deduplicate_customers([{'customer_id': ' c001 ', 'name': 'Alice Smith', 'email': '[email protected]', 'spend': 120, 'active': True}, {'customer_id': 'C001', 'name': 'Alice M Smith', 'email': '[email protected]', 'spend': 95.5, 'active': False}, {'customer_id': 'c001', 'name': 'Alice Smith', 'email': '[email protected]', 'spend': 200, 'active': False}])

Output: [{'customer_id': 'C001', 'name': 'Alice M Smith', 'email': '[email protected]', 'spend': 200, 'active': True}]

Example 2

Input: deduplicate_customers([{'customer_id': 'B002', 'name': 'Bob', 'email': '[email protected]', 'spend': 50, 'active': False}, {'customer_id': 'A001', 'name': 'Ana Ruiz', 'email': '[email protected]', 'spend': 300, 'active': True}])

Output: [{'customer_id': 'A001', 'name': 'Ana Ruiz', 'email': '[email protected]', 'spend': 300, 'active': True}, {'customer_id': 'B002', 'name': 'Bob', 'email': '[email protected]', 'spend': 50, 'active': False}]

Example 3

Input: deduplicate_customers([])

Output: []

Hints

Hint 1

Start by normalizing each customer_id (strip + uppercase) so you can group records that belong to the same customer.

Hint 2

For the email field, use a frequency counter (collections.Counter) per group, then resolve ties with min() on the candidates that share the top count.

Hint 3

Build each canonical record field-by-field from the group, then collect all canonical records into a list and sort by customer_id before returning.