Problem · Array

First-Available Seat Booking

Learn this problem
EasyTarget logoTargetFULLTIMEOA

Problem statement

A ticket-booking system has n available seats numbered from 1 through n. It receives q booking requests.

Process the requests in their given order. Each assignment completes atomically before the next request. For each request, assign and return the lowest-numbered seat that has not already been assigned. If no seat remains, return -1 for that request.

Return an array containing the result of every request.

Implement bookFirstAvailableSeats(n, q).

Function

bookFirstAvailableSeats(n: int, q: int) → int[]

Examples

Example 1

n = 3q = 5return = [1,2,3,-1,-1]

The first three requests receive seats 1, 2, and 3. The remaining requests find no available seat.

Example 2

n = 1q = 1return = [1]

The single request receives the only available seat.

Constraints

  • 1 <= n <= 100000.
  • 1 <= q <= 100000.

More Target problems

drafts saved locally
public int[] bookFirstAvailableSeats(int n, int q) {
  // Write your code here.
}
n3
q5
expected[1,2,3,-1,-1]
checking account