25Split stays and group stays
25 Split stays and group stays
Airbnb candidates report a split stay question: listings with the days each one is free, and a date range to cover with two of them, one for the first part of the trip and the other for the rest. One was asked for an API endpoint returning every such pair, brute force first, with extra credit for a bitmask version (1 report). Another had to cut the range into a left part and a right part held by different listings, return the pairs with no duplicates, and do it bug free live, with every test passing (1 report). A third source gives only its name (1 source). Group stay questions give a group size and listings with capacities and ask for the best set of listings that fits everyone, each listing used at most once. One filters to a target neighborhood and returns an empty list when nothing fits (1 report). The two group stay reports break ties in opposite orders: fewest listings first, then least total capacity (1 report), or least capacity first, then fewest listings (1 report). In the interview, ask. Here the order is fixed in level 2. This is practice in that shape, not a leaked question.
Set a 60-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/25_stays -k level1. Earlier levels must keep passing. Hold yourself to the bar in
q-abnb-080: check the boundaries by hand before you run the tests.
listings maps a name to a dict with free (the nights it's free, as integers in any order),
capacity (how many guests it sleeps) and neighborhood. A stay from start to end includes
both nights.
Level 1. split_stays(listings, start, end) returns a list of (first, second) tuples, one for
every pair of different listings where you can sleep at first from night start through some night k, then at second
from night k + 1 through end, with at least one night at each.
- A pair appears once, however many nights
kwould work. (first, second)and(second, first)are different trips. The list is sorted.- A listing that is free for the whole stay is in no pair: the guest would just book it.
- A one-night stay has no splits.
A brute force over pairs and split nights is a fine start. Then try finding, for each listing, how
far its free run from start reaches and where its free run into end begins, which makes each
pair one comparison.
Level 2. group_stay(listings, guests, neighborhood) picks listings in neighborhood whose
capacities add up to at least guests, each used at most once, and returns their names sorted, or
[] if no set fits. When several sets fit, the best one has:
- the fewest listings, then
- the least total capacity, then
- the alphabetically first list of names (compare the sorted lists).
Trying every set is fine for the 15 or so listings in one neighborhood. Be ready to say what you'd do with thousands.
Level 3. A search page shows options, not just the best one.
stay_options(listings, guests, neighborhood, start, end, limit) uses only listings in
neighborhood that are free every night from start to end. An option is a set of them that
fits the group where every listing is needed: drop any one and it no longer fits. Return up to
limit options, best first by the level 2 order, each a sorted list of names.
Follow-ups: the bitmask version of level 1, a trip that needs more than two listings (the fewest moves that cover it), and many searches against the same calendars.