FastPrepBuild Blocks from a Starting Position
Problem · Binary Search

Build Blocks from a Starting Position

Learn this problem
MediumTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem statement

Given an infinite number line, you would like to build few blocks and obstacles on it. Specifically, you have to implement code which supports two types of operations:

  • [1, x] - builds an obstacle at coordinate x along the number line. It is guaranteed that coordinate x does not contain any obstacles when the operation is performed.
  • [2, x, size] - checks whether it's possible to build a block of size size beginning at position x. For example, for size = 2 and x = 0, it will check 0 and 1 on the number line for obstacles. Returns 1 if it is possible, i.e. there are no obstacles at the occupied coordinates, and return 0 otherwise. Please note that this operation does not actually build the block, it only checks whether a block can be built.

Given an array of operations containing both types of operations above, your task is to return a binary string representing the outputs for all [2, x, size] operations.

Function

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

Examples

Example 1

operations = [[1, 2], [1, 5], [2, 3, 2], [2, 3, 3], [2, 1, 1], [2, 1, 2]]return = "1010"

For

operations = [[1, 2],
              [1, 5],
              [2, 3, 2],
              [2, 3, 3],
              [2, 1, 1],
              [2, 1, 2]]

FastPrep-authored deterministic derivation: The source image is cropped before the example output. Applying the visible rules gives solution(operations) = "1010".

Constraints

  • FastPrep execution-adapter constraints (not shown in the source image):
  • Each operation is either [1, x] or [2, x, size].
  • Every size in a type-2 operation is positive.
  • Each type-1 operation targets a coordinate that does not currently contain an obstacle.

More Tiktok problems

drafts saved locally
public String buildBlocksFromPosition(int[][] operations) {
    // Write your code here.
}
operations[[1, 2], [1, 5], [2, 3, 2], [2, 3, 3], [2, 1, 1], [2, 1, 2]]
expected"1010"
checking account