Verify Which Orders Have Received Payments
Arrays & HashingAuto-graded · 18 test casesLast 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 —123and"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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Comparing every order against every payment directly costs O(n·m), which is too slow for large inputs. The better pattern is a hash join:
- Build a set of every
"booking_id"that appears inpayments. This is the index phase — O(m). - Walk
ordersin order and test whether each order's"booking_id"is in the set. This is the probe phase — O(n) average.
Why It Is Correct
- If the function returns
Truefor an order, its id is in the set, which means at least one payment referenced it. - If any payment references an order, that payment's
"booking_id"was inserted during the index phase, so the probe returnsTrue. - The output is produced by a single in-order pass over
orders, so indexialways corresponds toorders[i]. - Duplicate payments for the same order collapse into one set entry, which is exactly right for an "at least one" question.
Edge Cases
- Empty
orders→ return[]. - Empty
payments→ every entry isFalse. - Orphan payments never get probed and don't affect the output.
- If
"booking_id"appears more than once inorders, each occurrence gets its own output entry with the same value.
Complexity
- Time: O(n + m) average — O(m) to build the set, O(1) per lookup × n lookups.
- Space: O(u + n), where u ≤ m is the number of distinct paid booking ids, and n is the output list.
Reference Solution
from typing import List, Dict
def orders_with_payments(orders: List[Dict], payments: List[Dict]) -> List[bool]:
# Index phase: collect every booking_id that has at least one payment.
paid_ids = {p["booking_id"] for p in payments}
# Probe phase: walk orders in input order, O(1) average lookup each.
return [o["booking_id"] in paid_ids for o in orders]
Follow-up: Multiple Payments per Order
The base solution already handles multiple payments correctly because the set deduplicates them. Richer questions — such as "has the full amount been paid?" — require aggregation. Upgrade the set to a defaultdict that sums amounts or counts records per "booking_id". Complexity stays O(n + m). Real-world concerns worth raising: partial payments, refunds as negative amounts or status-filtered records, currency normalization, and deduplication by a payment id to handle retries.