KatInterview

Seat Row Simulator: Leftmost Block Booking and Group Release

Data Structure DesignAuto-graded · 16 test cases
HardAuto-gradedData Structure Designsegment-treelazy-propagationsimulation

Last updated

Simulate a row of seats where each booking takes the leftmost free block of a given length and a release frees all of a group's seats, efficiently on large inputs.

Seat Row Simulator

You manage one row of n seats, indexed 0 through n - 1. At the start every seat is empty. You receive a list of requests and must process them in order. Each request produces exactly one integer, and you return these integers as a list in request order.

There are two kinds of request:

  • Reserve [1, size, group]: find the leftmost run of size adjacent empty seats. Among all windows of size consecutive empty seats, choose the one with the smallest starting index. Give all size seats to group and output that starting index. If no such window exists, change nothing and output -1.
  • Release [2, group]: empty every seat currently held by group, regardless of how many separate reservations produced them. Output the number of seats freed, or 0 if the group holds nothing.

Function

Implement simulate_seat_row(n, requests). It returns a list of integers, one per request, in the same order as requests.

Details

  • The chosen window may start in the middle of a longer empty stretch. It only has to be size consecutive empty seats with the smallest possible first index.
  • A group may hold several disjoint blocks from reservations made at different times. A single release frees all of them.
  • A group that already holds seats may reserve more. Its existing seats are unaffected.
  • After a release, the same group identifier may reserve again as if it were new.
  • A failed reservation (-1) does not modify the row or any group's holdings.

Constraints

  • 1 <= n <= 100000
  • 0 <= len(requests) <= 100000
  • In every reserve request, 1 <= size <= n.
  • Group identifiers are opaque, hashable tokens (for example, strings).
  • Aim for roughly O(log n) work per reserve. A release should cost O(log n) per block the group holds. Rescanning the whole row on every request is too slow at the upper limits.

Examples

Example 1

Input: simulate_seat_row(10, [[1, 3, 'A'], [1, 2, 'B'], [1, 2, 'C'], [2, 'B'], [1, 3, 'D'], [1, 2, 'E'], [2, 'A'], [1, 4, 'F']])

Output: [0, 3, 5, 2, 7, 3, 3, -1]

Hints

Hint 1

The hard part is finding the leftmost window of `size` empty seats quickly. Think about a tree over the seats where each node summarizes its range, so you can rule out whole halves at once.

Hint 2

Per node, store the length of the empty run touching its left edge, the empty run touching its right edge, and the longest empty run inside. A run crossing the midpoint is the left child's right-edge run plus the right child's left-edge run.

Hint 3

To search, go left if the left child holds a long enough run. Otherwise check the run crossing the midpoint, and only then go right. Mark and unmark whole ranges with lazy assignment, and remember each group's (start, size) blocks so a release can undo them.