Problem · Intervals

Obstacle Placement Queries

Learn this problem
MediumTiktokOA
See Tiktok hiring insights

Problem statement

You are given an infinite number line and an array operations. Process the operations in order while maintaining the coordinates that contain obstacles.

  • [1, x]: Place an obstacle at coordinate x. Coordinate x is guaranteed to contain no obstacle when this operation is performed.
  • [2, x, size]: Check whether a block of length size can begin at coordinate x. The block would occupy every integer coordinate from x through x + size - 1. Append '1' to the answer if none of those coordinates contains an obstacle; otherwise append '0'. This operation only checks feasibility and does not place the block.

Return the binary string formed by the results of all type-2 operations in their original order.

Function

obstaclePlacementQueries(operations: int[][]) → String

Examples

Example 1

operations = [[2, 0, 2], [1, 1], [2, 0, 2], [2, 2, 2]]return = "101"
  1. [2, 0, 2] checks coordinates 0 and 1. There are no obstacles, so append 1.
  2. [1, 1] places an obstacle at coordinate 1.
  3. [2, 0, 2] now intersects the obstacle at coordinate 1, so append 0.
  4. [2, 2, 2] checks coordinates 2 and 3. Both are free, so append 1.

More Tiktok problems

drafts saved locally
public String obstaclePlacementQueries(int[][] operations) {
  // write your code here
}
operations[[2, 0, 2], [1, 1], [2, 0, 2], [2, 2, 2]]
expected"101"
checking account