KatInterview

Slot Availability Queries on a Sparse Number Line

Data Structure DesignModel answer
MediumModel answerData Structure Designbinary-searchsorted-structuresfenwick-tree

Last updated

Handle an online stream of point-blocking and window-clearance queries on a huge integer line, and return a bit string with one result per query.

Blocked Cells and Clearance Queries

A warehouse aisle is modeled as an unbounded integer number line. Negative positions are allowed. At the start, every position is open.

You receive a list of operations and must process them in order:

  • ["block", p]: position p becomes permanently blocked. It is guaranteed that p is not blocked at the time this operation runs.
  • ["probe", c, r]: a pallet is considered whose footprint is centered at position c and reaches r - 1 positions to the left and to the right. Decide whether all positions from c - (r - 1) through c + (r - 1) (both ends included) are currently open. A probe is read-only and never blocks anything.

Return a single string that joins the answers of all probes in order: "1" if the window is fully open, "0" if at least one position in it is blocked.

Function signature

def probe_windows(operations: list[list]) -> str:

Rules

  • A probe only reflects block operations that appear earlier in the list.
  • The window has 2 * r - 1 positions. When r = 1, only position c is examined.
  • Each probe produces exactly one character. If the list has no probes, return "".

Examples

operations = [["block", 2], ["probe", 0, 3], ["probe", 0, 2],
              ["block", -5], ["probe", -3, 3], ["probe", -3, 2]]
output = "0101"

The first probe covers -2..2 and includes the blocked 2, giving 0. The second covers -1..1, all open, giving 1. After blocking -5, the third probe covers -5..-1 and hits it, giving 0. The fourth covers -4..-2, open, giving 1.

operations = [["probe", 7, 1], ["block", 7], ["probe", 7, 1]]
output = "10"

Constraints

  • 1 <= len(operations) <= 10^5
  • -10^9 <= c, p <= 10^9, 1 <= r <= 10^9
  • No position is blocked more than once.
  • Window endpoints fit comfortably in a 64-bit integer.

What to provide

Explain your approach, give working code, and state the time and space complexity. Discuss how your solution behaves when there are up to 10^5 operations, not only on small inputs.

Hints

Hint 1

The line is far too large to store, but only the blocked positions matter. Ask what you need to know about them to decide whether any lies inside [lo, hi].

Hint 2

A window is fully open exactly when the smallest blocked position that is >= lo is either missing or greater than hi. Which search primitive finds that element?

Hint 3

A plain sorted Python list makes each insertion cost O(n) because of shifting. To get guaranteed O(log n) operations, think about reading all block positions ahead of time, compressing them to ranks, and counting blocked positions in a rank range with a Fenwick tree that only includes blocks that have already happened.