Frontend School

Optimal Dynamic Flight Seating Revenue

DSA
hard

Optimal Dynamic Flight Seating Revenue

Hard
Greedy

Overview

Ixigo is optimizing its dynamic pricing model for flight seats. For a popular flight, the airline pre-allocates a certain number of seats and takes group bookings. To maximize revenue, the airline employs a dynamic pricing strategy: each time a block of seats is sold to a group, the base price for all subsequent seat sales increases by a fixed increment. Your task is to determine the maximum total revenue the airline can achieve by strategically accepting and ordering group bookings. This problem is critical for maximizing profitability on high-demand routes while managing limited inventory.

What you need to do / Task

Implement a function calculateMaxRevenue that determines the maximum possible revenue the airline can generate. The function should:

  1. Accept a fixed number of total seats available on the flight, an initial base price for all seats, a price increment applied after each group booking, and a list of group booking requests.
  2. For each group booking request, the airline knows the number of seats requested and the maximum price per seat that the group is willing to pay.
  3. A group booking can only be accepted if all its requested seats can be purchased at or below its specified maximum per-seat price, given the current dynamic seat price.
  4. If a group is accepted, its requested seats are deducted from the total available seats, and the dynamic seat price for subsequent sales increases by the increment.
  5. Maximize the total revenue from accepted group bookings.

Input & output

Function Signature: function calculateMaxRevenue(totalSeats: number, basePrice: number, increment: number, groups: number[][]): number

  • totalSeats: An integer representing the total number of seats available on the flight.
  • basePrice: An integer representing the initial price for a single seat before any sales.
  • increment: An integer representing the amount by which the dynamic seat price increases after each successful group booking.
  • groups: A 2D array of integers, where each inner array [k_i, p_i] represents a group booking request:
    • k_i: The number of seats requested by group i.
    • p_i: The maximum price per seat group i is willing to pay.
  • Return: An integer representing the maximum total revenue achievable.

Clarifications

  • Groups are processed one by one. Once a group is accepted or rejected, the decision is final.
  • The increment applies to the per-seat price.
  • If totalSeats becomes 0, no more groups can be accepted.
  • The basePrice and increment are always positive. k_i and p_i are always positive.

Examples

1

Example 1

Input:

totalSeats = 10, basePrice = 100, increment = 10, groups = [[3, 130], [5, 110], [2, 120]]

Output:

1080

Note:

  1. Initial: availableSeats = 10, currentPrice = 100, totalRevenue = 0.
  2. Sorted groups (by p_i ascending, then k_i ascending): [[5, 110], [2, 120], [3, 130]].
  3. Process [5, 110]:
    • Request: 5 seats, max 110/seat. Current price: 100.
    • Cost: 5 * 100 = 500. Max allowed: 5 * 110 = 550. 500 <= 550. Accept.
    • availableSeats = 10 - 5 = 5. totalRevenue = 500.
    • currentPrice = 100 + 10 = 110.
  4. Process [2, 120]:
    • Request: 2 seats, max 120/seat. Current price: 110.
    • Cost: 2 * 110 = 220. Max allowed: 2 * 120 = 240. 220 <= 240. Accept.
    • availableSeats = 5 - 2 = 3. totalRevenue = 500 + 220 = 720.
    • currentPrice = 110 + 10 = 120.
  5. Process [3, 130]:
    • Request: 3 seats, max 130/seat. Current price: 120.
    • Cost: 3 * 120 = 360. Max allowed: 3 * 130 = 390. 360 <= 390. Accept.
    • availableSeats = 3 - 3 = 0. totalRevenue = 720 + 360 = 1080.
    • currentPrice = 120 + 10 = 130. Final totalRevenue = 1080.
2

Example 2

Input:

totalSeats = 5, basePrice = 50, increment = 5, groups = [[2, 50], [3, 60], [1, 40]]

Output:

265

Note:

  1. Initial: availableSeats = 5, currentPrice = 50, totalRevenue = 0.
  2. Sorted groups: [[1, 40], [2, 50], [3, 60]].
  3. Process [1, 40]:
    • Request: 1 seat, max 40/seat. Current price: 50.
    • Cost: 1 * 50 = 50. Max allowed: 1 * 40 = 40. 50 > 40. Reject.
    • availableSeats = 5. totalRevenue = 0. currentPrice remains 50.
  4. Process [2, 50]:
    • Request: 2 seats, max 50/seat. Current price: 50.
    • Cost: 2 * 50 = 100. Max allowed: 2 * 50 = 100. 100 <= 100. Accept.
    • availableSeats = 5 - 2 = 3. totalRevenue = 100.
    • currentPrice = 50 + 5 = 55.
  5. Process [3, 60]:
    • Request: 3 seats, max 60/seat. Current price: 55.
    • Cost: 3 * 55 = 165. Max allowed: 3 * 60 = 180. 165 <= 180. Accept.
    • availableSeats = 3 - 3 = 0. totalRevenue = 100 + 165 = 265.
    • currentPrice = 55 + 5 = 60. Final totalRevenue = 265.
3

Example 3

Input:

totalSeats = 3, basePrice = 100, increment = 10, groups = [[2, 120], [2, 110], [1, 150]]

Output:

310

Note:

  1. Initial: availableSeats = 3, currentPrice = 100, totalRevenue = 0.
  2. Sorted groups: [[2, 110], [2, 120], [1, 150]] (Sorted by p_i ascending, then k_i ascending).
  3. Process [2, 110]:
    • Request: 2 seats, max 110/seat. Current price: 100.
    • Cost: 2 * 100 = 200. Max allowed: 2 * 110 = 220. 200 <= 220. Accept.
    • availableSeats = 3 - 2 = 1. totalRevenue = 200.
    • currentPrice = 100 + 10 = 110.
  4. Process [2, 120]:
    • Request: 2 seats, max 120/seat. Current price: 110.
    • availableSeats = 1. Not enough seats for 2. Reject.
    • availableSeats = 1. totalRevenue = 200. currentPrice remains 110.
  5. Process [1, 150]:
    • Request: 1 seat, max 150/seat. Current price: 110.
    • availableSeats = 1. Enough seats.
    • Cost: 1 * 110 = 110. Max allowed: 1 * 150 = 150. 110 <= 150. Accept.
    • availableSeats = 1 - 1 = 0. totalRevenue = 200 + 110 = 310.
    • currentPrice = 110 + 10 = 120. Final totalRevenue = 310.

Constraints

Constraints & assumptions

  • 1 <= totalSeats <= 10^5
  • 1 <= basePrice <= 10^6
  • 1 <= increment <= 10^6
  • 1 <= groups.length <= 10^5
  • 1 <= k_i <= totalSeats
  • 1 <= p_i <= 10^6
  • All input values are positive integers.
  • The total revenue will fit within a standard 64-bit integer type (e.g., JavaScript's number type, which uses double-precision floating-point format, can precisely represent integers up to 2^53 - 1, sufficient for 10^16).

What interviewers evaluate

  • Correctness of Greedy Strategy: Can the candidate identify and justify the optimal greedy choice for ordering group bookings?
  • Algorithm Efficiency: The solution should be efficient, handling large inputs within typical time limits (e.g., O(M log M) or O(M)).
  • Edge Case Handling: Correctly handles scenarios like insufficient seats, groups exceeding their budget, or all seats being sold.
  • Code Clarity and Structure: Well-organized, readable code with appropriate variable names and comments where necessary.

Loading editor...