Unify Two Yearly Sales Record Lists With a Mismatched Key
SQL & DataAuto-graded · 13 test casesLast updated
Tests normalizing records from two sources with a misspelled field name, casting a numeric field to float, and concatenating the lists in order without mutating inputs.
Unify Two Yearly Sales Record Lists
A shop exports its sales as one list of records per year. Each record is a dictionary. The two exports are meant to share a layout, but the second one contains a misspelled field name.
- Records in the earlier export have the keys
order_id,customer_idandamount. - Records in the later export have the keys
orderid(no underscore),customer_idandamount.
Write a function that takes both lists and returns a single combined list of records that all follow the same layout.
Rules
- Every output record is a new dictionary with exactly three keys:
order_id,customer_idandamount. - For records from the later export, the value stored under
orderidbecomes the value oforder_id. - The
amountof every record is converted to a float, whichever export it came from and whatever numeric type it had on input. - The
order_idandcustomer_idvalues are copied unchanged, with no type conversion. - The output holds all records of the earlier export first, then all records of the later export, each group in its original order.
- Nothing is removed, merged or deduplicated. If the same customer appears in both lists, both records stay in the result.
- Any other keys present in an input record are ignored and do not appear in the output.
- The input lists and their dictionaries must not be modified.
- If both lists are empty, return an empty list.
Input
earlier_orders: a list of dictionaries using the keyorder_id.later_orders: a list of dictionaries using the keyorderid.
Output
A list of dictionaries, each with the keys order_id, customer_id and amount, as described above.
Constraints
0 <= len(earlier_orders), len(later_orders)- Every record in
earlier_orderscontainsorder_id,customer_idandamount. - Every record in
later_orderscontainsorderid,customer_idandamount. - Every
amountcan be converted withfloat(...). - Linear time in the total number of records is expected.
Examples
Example 1
Input: unify_yearly_orders([{'order_id': 101, 'customer_id': 'C001', 'amount': 120.5}, {'order_id': 102, 'customer_id': 'C002', 'amount': 75}, {'order_id': 103, 'customer_id': 'C003', 'amount': 140}], [{'orderid': 201, 'customer_id': 'C001', 'amount': 110}, {'orderid': 202, 'customer_id': 'C004', 'amount': 95}, {'orderid': 203, 'customer_id': 'C005', 'amount': 180}])
Output: [{'order_id': 101, 'customer_id': 'C001', 'amount': 120.5}, {'order_id': 102, 'customer_id': 'C002', 'amount': 75}, {'order_id': 103, 'customer_id': 'C003', 'amount': 140}, {'order_id': 201, 'customer_id': 'C001', 'amount': 110}, {'order_id': 202, 'customer_id': 'C004', 'amount': 95}, {'order_id': 203, 'customer_id': 'C005', 'amount': 180}]
Hints
Hint 1
Handle each list separately: the only difference between them is which key holds the order identifier.
Hint 2
Build a fresh dictionary for each record with exactly the three target keys, instead of editing the input dictionary.
Hint 3
Convert amount with float(...) for every record, then append the earlier list's records before the later list's records.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The two inputs differ only in the name of the key that holds the order identifier. So each list is processed on its own with the right source key, and the results are appended to one output list.
- Create an empty result list.
- For each record in
earlier_orders, build a new dictionary withorder_idread fromorder_id,customer_idcopied, andamountconverted withfloat(...). Append it. - Do the same for each record in
later_orders, but read the identifier fromorderid. - Return the result.
Why it is correct
- Every output dictionary is built with the same three keys, so the combined list has one consistent layout. Extra input keys are dropped automatically because they are never copied.
- Casting every
amountwithfloatmakes the numeric type uniform, whether the input was an int, a float or a numeric string. - The earlier list is fully processed before the later one, and each is traversed in order, so the output order is earlier records followed by later records.
- Since we build new dictionaries and only read from the inputs, the inputs are never mutated.
- Nothing is filtered or merged, so duplicate customers across the two lists remain as separate records.
- Empty inputs simply produce no iterations, so two empty lists give
[].
Complexity
With n earlier records and m later records:
- Time: O(n + m), constant work per record.
- Space: O(n + m) for the output list.
Reference solution
from typing import Dict, List
def unify_yearly_orders(earlier_orders: List[Dict], later_orders: List[Dict]) -> List[Dict]:
result = []
for row in earlier_orders:
result.append({
"order_id": row["order_id"],
"customer_id": row["customer_id"],
"amount": float(row["amount"]),
})
for row in later_orders:
result.append({
"order_id": row["orderid"],
"customer_id": row["customer_id"],
"amount": float(row["amount"]),
})
return result