KatInterview

Deduplicate and Reconcile Customer Records

SQL & DataAuto-graded · 13 test cases
MediumAuto-gradedSQL & Datadata cleaningdeduplicationaggregation

Last updated

Tests the ability to clean a list of messy customer records by deduplicating on a canonical key, resolving field conflicts with explicit priority rules, and returning a sorted, normalized result.

Deduplicate and Reconcile Customer Records

You are given a list of customer records extracted from multiple data sources. Because records were inserted by different systems over time, the same customer may appear more than once with conflicting or missing field values.

Each record is a dictionary with the following keys:

KeyTypeNotes
"id"strA non-empty alphanumeric customer ID. Case-insensitive — "C001" and "c001" are the same customer.
"name"str or NoneCustomer's full name. May be None or an empty string "".
"email"str or NoneCustomer's email. May be None or an empty string "".
"score"int or NoneAn engagement score. May be None.

Your task is to implement reconcile_customers(records) which returns a cleaned, deduplicated list of customer records.

Deduplication Key

Two records refer to the same customer if and only if their "id" fields are equal after lower-casing.

Field Reconciliation Rules (applied per customer group)

After grouping all records for the same customer, produce a single merged record using these rules in order:

  1. "id" — Use the lower-cased version of the ID.
  2. "name" — Use the first non-empty, non-None value encountered, where "first" means the record appears earliest in the input list. If all values are empty/None, use None.
  3. "email" — Same rule as name: first non-empty, non-None value by input order. If all are empty/None, use None.
  4. "score" — Use the maximum non-None value across all records for that customer. If all values are None, use None.

Output Requirements

  • Return a list of merged records (one per unique customer ID).
  • Sort the output list ascending by the normalized (lower-cased) "id" field.
  • Every returned record must contain exactly the four keys: "id", "name", "email", "score".

Constraints

  • 0 ≤ len(records) ≤ 2000
  • Each record is guaranteed to contain all four keys ("id", "name", "email", "score").
  • "id" is always a non-empty string containing only ASCII letters and digits.
  • "name" and "email" are either None, "", or a non-empty string.
  • "score" is either None or a non-negative integer.
  • Input order is the order records appear in the input list (index 0 first).

Examples

Example 1

Input: reconcile_customers([{'id': 'C001', 'name': 'Alice', 'email': '[email protected]', 'score': 80}, {'id': 'c001', 'name': None, 'email': '', 'score': 95}, {'id': 'C002', 'name': 'Bob', 'email': None, 'score': None}])

Output: [{'id': 'c001', 'name': 'Alice', 'email': '[email protected]', 'score': 95}, {'id': 'c002', 'name': 'Bob', 'email': None, 'score': None}]

Example 2

Input: reconcile_customers([])

Output: []

Example 3

Input: reconcile_customers([{'id': 'X10', 'name': None, 'email': None, 'score': None}, {'id': 'X10', 'name': '', 'email': '[email protected]', 'score': 10}, {'id': 'X10', 'name': 'Xander', 'email': '[email protected]', 'score': 5}])

Output: [{'id': 'x10', 'name': 'Xander', 'email': '[email protected]', 'score': 10}]

Hints

Hint 1

Group records by lowercased ID while preserving original input order — an ordered dict or a list-of-indices approach works well.

Hint 2

For name and email, a simple loop that returns the first truthy string value handles both None and empty-string cases in one pass.

Hint 3

For score, use the built-in max() with a default of None, filtering out None values first; if the filtered list is empty, the result is None.