Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The same customer exists in the CRM, the billing system and the support tool. No one of them is complete: billing has the email, the CRM has the phone number, and support has whichever one somebody typed last. The customer dimension needs one row, and today it is built by taking whichever record was loaded last.
Write coalesce_records(records, key_field, source_priority, fields).
Function to write
coalesce_records(records: list[dict], key_field: str, source_priority: list[str], fields: list[str]) -> list[dict]One golden record per key, with each field's chosen value and the source it came from.
How to approach it
Sort each key's records into preference order once, then pick each field from the first that has it.
Sample cases
+ 2 held back until you submit
three systems, one customer
Every field comes from a different source, one value is blank rather than null, and one record has no key.
Input
Argument 1
| customer_id | source | name | phone | updated_at | |
|---|---|---|---|---|---|
| c1 | crm | Ada L. | None | 555-0100 | 2026-03-01 |
| c1 | billing | Ada Lovelace | ada@example.com | 2026-02-01 | |
| c1 | support | ada | ada@old.example.com | 555-0999 | 2026-03-05 |
| c2 | support | Grace | grace@example.com | None | 2026-03-02 |
| None | crm | nobody | None | None | 2026-03-02 |
Argument 2
'customer_id'Argument 3
[
'billing',
'crm',
'support'
]Argument 4
[
'name',
'email',
'phone'
]Returns
| customer_id | name | name_source | email_source | phone | phone_source | |
|---|---|---|---|---|---|---|
| c1 | Ada Lovelace | billing | ada@example.com | billing | 555-0100 | crm |
| c2 | Grace | support | grace@example.com | support | None | None |
no records
Nothing to coalesce returns an empty list rather than one empty golden record.
Input
Argument 1
[] (empty list)
Argument 2
'customer_id'Argument 3
[
'billing'
]Argument 4
[
'name'
]Returns
[] (empty list)
an unknown source
A source nobody ranked is trusted least, but still used when the ranked ones have nothing.
Input
Argument 1
| customer_id | source | name | updated_at |
|---|---|---|---|
| c9 | mystery | Unknown | 2026-03-09 |
| c9 | billing | None | 2026-01-01 |
Argument 2
'customer_id'Argument 3
[
'billing',
'crm'
]Argument 4
[
'name'
]Returns
| customer_id | name | name_source |
|---|---|---|
| c9 | Unknown | mystery |
Constraints
source_priority order. A source not listed is trusted least but is still used when no listed source has a usable value.updated_at, compared as text.None and not a string of only whitespace. A blank string is a field somebody cleared, not a value.<field>_source.None with a source of None, rather than being left out.Worked example
For c1 the CRM is missing the email, billing has a phone of three spaces, and support has an old email address. With billing ranked first, the email comes from billing, the phone from the CRM — billing's is blank and therefore not a value — and the name from billing as Ada Lovelace.
Picking a whole record instead would force a choice: billing gives the best email and no phone at all, the CRM gives the phone and no email. Whichever you pick, the customer row is missing a field that the company demonstrably holds. Field-level coalescing is the only version that produces a record more complete than any of its inputs, which is the entire reason for building one.
What this tests
Precedence expressed as a sort rather than as branches, and per-field rather than per-record selection. The provenance column is what turns a merged record from something to trust into something to check.
coalesce_records(records: list[dict], key_field: str, source_priority: list[str], fields: list[str]) -> list[dict]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.