In the Corporate Flight Bookings coding problem, you are given a set of bookings for n flights. Each booking is represented as [first, last, seats], meaning seats number of seats were booked for every flight from index first to last (inclusive). You need to return an array of size n containing the total number of seats booked for each flight.
Google and Amazon ask the Corporate Flight Bookings interview question to see if you can optimize a range-update problem. A naive approach would be to iterate through the range for every booking, which takes O(Bookings * n) time. The optimal solution uses a "Difference Array," reducing the complexity to O(Bookings + n). It evaluates your ability to use prefix sums for interval management.
The primary Array interview pattern here is the Difference Array / Prefix Sum.
n + 1.[i, j, k]:
k to arr[i-1].k from arr[j] (if j < n).p will correctly represent the total seats for flight p+1 because the "+k" starts the contribution and the "-k" ends it.Flights: 5, Bookings: [[1, 2, 10], [2, 3, 20]]
[0, 0, 0, 0, 0]arr[0] += 10, arr[2] -= 10. Array: [10, 0, -10, 0, 0]arr[1] += 20, arr[3] -= 20. Array: [10, 20, -10, -20, 0]1010 + 20 = 3030 - 10 = 2020 - 20 = 00 + 0 = 0
Result: [10, 30, 20, 0, 0].last flight in the booking range.Difference arrays are a "must-know" for range update problems. They turn a O(N) range update into an O(1) operation. Practice this alongside the "Sweep Line" algorithm, as they share similar concepts.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Zero Array Transformation I | Medium | Solve | |
| Range Addition | Medium | Solve | |
| Number of Ways to Split Array | Medium | Solve | |
| Taking Maximum Energy From the Mystic Dungeon | Medium | Solve | |
| Count Positions on Street With Required Brightness | Medium | Solve |