KatInterview

Determine Available Range on Number Line

Heaps, Sorting & SearchAuto-graded · 21 test cases
MediumAuto-gradedHeaps, Sorting & Searchheaps-sortingbinary-searchfenwick-tree

Last updated

Evaluate dynamic range-query operations on 1D coordinates to test whether continuous subsegments contain obstacles.

You are given a sequence of operations performed on a 1D grid of infinite length, where all integer positions start out unoccupied.

There are two types of operations:

  1. [1, x]: Place an obstacle at position x. If position x already contains an obstacle, this operation has no effect.
  2. [2, x, k]: Query whether a contiguous block of length k starting at x (i.e., covering positions x, x + 1, ..., x + k - 1) is completely free of obstacles. Append '1' to the result string if all positions in the interval are unoccupied, or '0' if at least one obstacle is present in that interval.

Execute all operations in the given order and return a single string containing the results of all type 2 query operations concatenated together.

Constraints

  • 1 <= len(operations) <= 10^5
  • Each element in operations is either [1, x] or [2, x, k].
  • 1 <= x <= 10^9
  • 1 <= k <= 10^9

Examples

Example 1

Input: process_operations([[1, 2], [1, 5], [2, 3, 2], [2, 3, 3], [2, 1, 1], [2, 1, 2]])

Output: '1010'

Example 2

Input: process_operations([[2, 10, 5], [1, 12], [1, 12], [2, 10, 2], [2, 10, 3], [2, 13, 100]])

Output: '1101'

Example 3

Input: process_operations([[1, 1]])

Output: ''

Hints

Hint 1

Since coordinate positions reach up to 10^9, you cannot maintain an array indexed directly by position.

Hint 2

Consider extracting all unique obstacle positions that appear in build operations to perform coordinate compression.

Hint 3

Alternatively, query whether the smallest obstacle position that is >= x falls inside the range [x, x + k - 1].