KatInterview

Verify Which Orders Have Received Payments

Arrays & HashingAuto-graded · 18 test cases
EasyAuto-gradedArrays & Hashinghash-setlookupaggregation

Last updated

Given two unsorted lists of records linked by a shared booking_id, return a boolean per order indicating whether at least one matching payment exists.

You manage a system that tracks customer orders and the payments collected against them. Both collections arrive as unsorted lists of dictionaries.

Each order dictionary contains at least a key "booking_id" that uniquely identifies the order. Each payment dictionary contains at least a key "booking_id" indicating which order the payment belongs to. Records may carry additional keys such as amount, currency, or timestamp; those extra fields are irrelevant to this task.

Write a function that returns a list of booleans, one per order, in the same order as the input orders list. The element at index i must be True if at least one payment dictionary has a "booking_id" equal to orders[i]["booking_id"], and False otherwise.

Payments whose "booking_id" does not correspond to any order are ignored. Multiple payments for the same order count as a single match. If the same "booking_id" appears more than once in orders, each occurrence produces its own entry in the output with the same answer.

Constraints

  • 0 ≤ len(orders) ≤ 10^6
  • 0 ≤ len(payments) ≤ 10^6
  • "booking_id" values may be integers or strings. Matching is exact — 123 and "123" are treated as different ids.
  • Your solution should run in O(n + m) average time, where n = len(orders) and m = len(payments).

Examples

Example 1

Input: orders_with_payments([{'booking_id': 'b1'}, {'booking_id': 'b2'}, {'booking_id': 'b3'}], [{'booking_id': 'b2'}])

Output: [False, True, False]

Example 2

Input: orders_with_payments([{'booking_id': 101}, {'booking_id': 102}], [{'booking_id': 101}, {'booking_id': 101}, {'booking_id': 999}])

Output: [True, False]

Example 3

Input: orders_with_payments([{'booking_id': 'a', 'room': 'A'}, {'booking_id': 'b', 'room': 'B'}], [{'booking_id': 'c', 'amount': 50}])

Output: [False, False]

Hints

Hint 1

Build a hash set of all booking_ids that appear in the payments list so each lookup costs O(1) on average.

Hint 2

Extra fields in either dictionary are irrelevant — only the booking_id value matters for matching.

Hint 3

Orphan payments (whose booking_id appears in no order) sit harmlessly in the set and are never probed, so they don't affect the result.