← Questions
Group booking across properties in one neighborhood
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A rental company houses groups in its properties. A group may be split across several properties, but all of them must be in the neighborhood the group asked for.
Implement BookingSystem:
-
BookingSystem(properties):propertiesis a list of[id, neighborhood, capacity](all strings;capacityis a positive integer). All properties start free. -
book(neighborhood, group_size) -> list[str]: choose free properties inneighborhoodfor the group, mark them booked, and return their ids sorted. Return[](and book nothing) if no choice fits.
Choose the set using these rules, in order:
- The total capacity is at least
group_size. - Use the fewest properties.
- Among those, use the smallest total capacity (fewest empty beds).
- If there is still a tie, return the lexicographically smallest sorted list of ids.
Constraints: up to 100 properties per neighborhood, capacity and group size up to 1,000.
Example 1
properties = [["a","soma","4"], ["b","soma","3"], ["c","soma","5"], ["d","soma","2"], ["e","mission","6"]]
book("soma", 7) -> ["a", "b"] # two properties, total 7; ties with ["c","d"], wins on ids
book("soma", 7) -> ["c", "d"] # a and b are booked now
book("soma", 1) -> [] # nothing free left in soma
book("mission", 6) -> ["e"]
Example 2
properties = [["p","h","10"], ["q","h","5"], ["r","h","5"]]
book("h", 4) -> ["q"] # one property either way; 5 wastes fewer beds than 10
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Write and test your solution herePython in your browser, the question's tests level by level, saved for you.Sign in to start coding
