FastPrepObstacle Placement Queries
Problem · Intervals

Obstacle Placement Queries

Learn this problem
MediumTiktok logoTiktokINTERNOA
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 end immediately before coordinate x. The block would occupy every integer coordinate from x - size through x - 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 = "110"
  1. [2, 0, 2] checks coordinates -2 and -1. There are no obstacles, so append 1.
  2. [1, 1] places an obstacle at coordinate 1.
  3. [2, 0, 2] still checks coordinates -2 and -1. Both are free, so append 1.
  4. [2, 2, 2] checks coordinates 0 and 1. Coordinate 1 contains an obstacle, so append 0.

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"110"
checking account