First-Available Seat Booking
Updated
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 Online Assessment questions
Similar questions at other companies
- First Successful Concurrent API Result Before a TimeoutApple · PHONE SCREENApple interview questions
- Concurrent Discounted Card PurchasesBrex · PHONE SCREEN
- Shared Counter Snapshot SumTesla · ONSITE INTERVIEWTesla interview questions
- Circuit Breaker State MachineWise · ONSITE INTERVIEW
- Schedule Buffered Video PlaybackNuro · PHONE SCREENNuro interview questions
- Simulate a Queued Multi-Rule Rate LimiterSnowflake · PHONE SCREENSnowflake interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions