This question evaluates resource allocation, combinatorial optimization, and online/streaming algorithm design within the Coding & Algorithms domain, requiring reasoning about assigning weight‑constrained cargo to flights under timing and cost constraints; it is commonly asked to assess algorithmic thinking about trade-offs between revenue and booking cost, constraint satisfaction, and handling dynamic inputs. The problem tests both conceptual understanding of optimization and online decision‑making and practical application skills for implementing incremental processing and robust handling of failed bookings.
You are given two streams/lists:
flight_id
depart_time
and
arrive_time
max_weight
(capacity)
cost
(paid only if you successfully book the flight)
cargo_id
weight
latest_arrival_time
(deadline)
revenue
(earned if delivered by the deadline)
A cargo job can be assigned to at most one booked flight. A flight can carry multiple cargo jobs as long as total assigned weight ≤ max_weight. A cargo job is deliverable on a flight if arrive_time ≤ latest_arrival_time.
The existing implementation is unprofitable because it books all flights regardless of whether there is profitable cargo to carry.
Design/implement changes so that the algorithm chooses which flights to book and which cargo to assign to maximize total profit:
You may output either:
Implement a class that processes events as they arrive:
onFlight(flight)
adds a new available flight.
onCargo(cargo)
adds a new available cargo job.
decide()
(or similar) updates the plan.
At any time, the class should be able to report the current planned profit and/or current bookings/assignments.
When you “apply” to book a flight, the booking can fail (e.g., another party booked it first). Your design should handle failed bookings gracefully (e.g., retry, re-plan, or treat it as removed from availability).