KatInterview

Unify Two Yearly Sales Record Lists With a Mismatched Key

SQL & DataAuto-graded · 13 test cases
EasyAuto-gradedSQL & Datadata-cleaningdictionariesschema-alignment

Last 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_id and amount.
  • Records in the later export have the keys orderid (no underscore), customer_id and amount.

Write a function that takes both lists and returns a single combined list of records that all follow the same layout.

Rules

  1. Every output record is a new dictionary with exactly three keys: order_id, customer_id and amount.
  2. For records from the later export, the value stored under orderid becomes the value of order_id.
  3. The amount of every record is converted to a float, whichever export it came from and whatever numeric type it had on input.
  4. The order_id and customer_id values are copied unchanged, with no type conversion.
  5. The output holds all records of the earlier export first, then all records of the later export, each group in its original order.
  6. Nothing is removed, merged or deduplicated. If the same customer appears in both lists, both records stay in the result.
  7. Any other keys present in an input record are ignored and do not appear in the output.
  8. The input lists and their dictionaries must not be modified.
  9. If both lists are empty, return an empty list.

Input

  • earlier_orders: a list of dictionaries using the key order_id.
  • later_orders: a list of dictionaries using the key orderid.

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_orders contains order_id, customer_id and amount.
  • Every record in later_orders contains orderid, customer_id and amount.
  • Every amount can be converted with float(...).
  • 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.