Block-Building Operations: Check Whether a Free Segment Starts at a Position
Capital OneSoftware Engineer · Online AssessmentLast updated
Process operations on a number line that either place a block at a position or ask whether a segment of a given length starting at a position is free of blocks, and return the answers as a string of ones and zeros. It tests handling interleaved updates and range checks efficiently over large coordinates.
Positions on a number line start out empty. Process a list of operations in order:
[1, x]: build a block at positionx.[2, x, k]: check whether something of lengthkcan be built starting at positionx, meaning that positionsx, x + 1, ..., x + k - 1hold no block. Record"1"if all of them are free and"0"otherwise. A check builds nothing.
Return the recorded characters concatenated in the order of the checks.
Function Signature
def process_operations(operations: list[list[int]]) -> str:
Rules
- Building at a position that already holds a block changes nothing.
- A check depends only on blocks built by earlier operations in the list.
- Only positions
xthroughx + k - 1matter for a check; blocks atx - 1orx + kdo not make it fail. - Return the empty string if there are no checks.
Constraints
1 <= len(operations) <= 10^5- Each operation is either
[1, x]or[2, x, k]. 1 <= x <= 10^9and1 <= k <= 10^9, sox + k - 1 <= 2 * 10^9 - 1, which fits in a 32-bit signed integer.
Examples
Example 1
Input: operations = [[1, 2], [1, 5], [2, 3, 2], [2, 3, 3], [2, 1, 1], [2, 1, 2]]
Output: "1010"
Blocks stand at 2 and 5. Positions 3 and 4 are free: "1". Positions 3, 4 and 5 include the block at 5: "0". Position 1 is free: "1". Positions 1 and 2 include the block at 2: "0".
Example 2
Input: operations = [[2, 10, 5], [1, 12], [1, 12], [2, 10, 2], [2, 10, 3], [2, 13, 100]]
Output: "1101"
The first check runs before any block exists: "1". The second build at 12 changes nothing. Positions 10 and 11 are free: "1". Positions 10 to 12 include the block at 12: "0". Positions 13 to 112 are free, and the block at 12 just before them does not matter: "1".
Example 3
Input: operations = [[1, 1]]
Output: ""
There are no checks.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.