Coding questions · Booking.com
Match Bookings to Payments
This question evaluates skill in matching records across unsorted collections, focusing on data structures, aggregation and handling one-to-one and one-to-many relationships between bookings and payments.
You are given two unsorted lists:
bookings: each record contains at least a uniquebooking_idpayments: each record contains at least abooking_idindicating which booking the payment belongs to
For each booking, determine whether a payment has been made for that booking.
Clarify your expected output, for example:
- return a boolean for a single booking, or
- return a mapping/list showing whether each booking has at least one matching payment
Follow-up:
- How would your approach change if one booking can have multiple payment records?
Part 1: Determine Whether Each Booking Has a Matching Payment
You are given two unsorted lists of booking IDs. bookings contains unique booking IDs, and payments contains the booking ID referenced by each payment record. For every booking in bookings, determine whether there is at least one payment with the same booking ID. Return the results as a list of booleans in the same order as bookings.
Constraints
0 <= len(bookings), len(payments) <= 200000bookingscontains unique integers0 <= booking_id <= 10^9paymentsmay contain duplicate booking IDspaymentsmay contain booking IDs that do not exist inbookings
Examples
Example 1
Input: ([101, 55, 78], [78, 101])
Output: [True, False, True]
Explanation: Bookings 101 and 78 appear in payments, but 55 does not.
Example 2
Input: ([1, 2, 3], [3, 3, 4, 1])
Output: [True, False, True]
Explanation: Duplicate payments for booking 3 still only mean True. Payment 4 is ignored because it has no matching booking.
Hints
- Checking every payment for every booking is too slow for large inputs. Can you preprocess
paymentsfor fast lookup? - The output order must match the original
bookingslist.
Part 2: Count Payment Records for Each Booking
You are given two unsorted lists of booking IDs. bookings contains unique booking IDs, and payments contains the booking ID referenced by each payment record. A single booking can now have multiple payment records. For every booking in bookings, return how many payment records belong to it. The result must be in the same order as bookings.
Constraints
0 <= len(bookings), len(payments) <= 200000bookingscontains unique integers0 <= booking_id <= 10^9paymentsmay contain duplicate booking IDspaymentsmay contain booking IDs that do not exist inbookings
Examples
Example 1
Input: ([101, 55, 78], [78, 101, 78, 78])
Output: [1, 0, 3]
Explanation: Booking 101 has 1 payment, 55 has none, and 78 has 3.
Example 2
Input: ([1, 2, 3], [3, 3, 4, 1, 2, 2, 2])
Output: [1, 3, 2]
Explanation: Booking 1 appears once, 2 appears three times, and 3 appears twice. Payment 4 is ignored.
Hints
- Instead of only tracking whether a payment exists, track how many times each payment booking ID appears.
- A hash map/dictionary can store the frequency of each booking ID in
payments.