There exists an infinite number line, with its origin at 0 and extending towards the positive x-axis.
You are given a 2D array queries, which contains two types of queries:
queries[i] = [1, x]. Build an obstacle at distance x from the origin. It is guaranteed that there is no obstacle at distance x when the query is asked.queries[i] = [2, x, sz]. Check if it is possible to place a block of size sz anywhere in the range [0, x] on the line, such that the block entirely lies in the range [0, x]. A block cannot be placed if it intersects with any obstacle, but it may touch it. Note that you do not actually place the block. Queries are separate.Return a boolean array results, where results[i] is true if you can place the block specified in the ith query of type 2, and false otherwise.
Example 1:
Input: queries = [[1,2],[2,3,3],[2,3,1],[2,2,2]]
Output: [false,true,true]
Explanation:

For query 0, place an obstacle at x = 2. A block of size at most 2 can be placed before x = 3.
Example 2:
Input: queries = [[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]]
Output: [true,true,false]
Explanation:

x = 7 for query 0. A block of size at most 7 can be placed before x = 7.x = 2 for query 2. Now, a block of size at most 5 can be placed before x = 7, and a block of size at most 2 before x = 2.Constraints:
1 <= queries.length <= 15 * 1042 <= queries[i].length <= 31 <= queries[i][0] <= 21 <= x, sz <= min(5 * 104, 3 * queries.length)x when the query is asked.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long straight road with some obstacles and you receive requests asking if you can place a block of a specific size before a certain point without hitting anything. The brute force approach looks at the exact stretch of road mentioned in each request and exhaustively checks every possible spot to see if the block fits.
Here's how the algorithm would work step-by-step:
def solve_block_placement_queries(obstacles, queries):
query_answers = []
# Iterate through each query to independently evaluate if a block can be placed
for current_query in queries:
if current_query[0] == 1:
obstacle_position = current_query[1]
obstacles.append(obstacle_position)
obstacles.sort()
else:
stopping_limit = current_query[1]
block_size = current_query[2]
is_block_placed = False
# Check every possible starting coordinate from the beginning of the road
for start_coordinate in range(stopping_limit - block_size + 1):
end_coordinate = start_coordinate + block_size
has_obstacle_overlap = False
for current_obstacle in obstacles:
if start_coordinate <= current_obstacle < end_coordinate:
has_obstacle_overlap = True
break
# Stop searching early once we find a valid position that fits the block
if not has_obstacle_overlap:
is_block_placed = True
break
query_answers.append(is_block_placed)
# Return the collected boolean answers for all the block placement queries
return query_answersTo efficiently answer whether a block of a certain size can fit into available empty spaces, we can think of the problem in terms of distances between obstacles. By grouping these distances and using a smart searching technique, we can quickly determine if any gap is large enough to hold the requested block.
Here's how the algorithm would work step-by-step:
def get_block_placement_results(obstacle_positions, placement_queries):
sorted_obstacles = sorted(obstacle_positions)
# We precompute gaps to avoid repeatedly scanning obstacles for every query.
max_gap_to_position = [0] * len(sorted_obstacles)
max_gap_to_position[0] = sorted_obstacles[0]
for obstacle_index in range(1, len(sorted_obstacles)):
current_gap = sorted_obstacles[obstacle_index] - sorted_obstacles[obstacle_index - 1]
max_gap_to_position[obstacle_index] = max(max_gap_to_position[obstacle_index - 1], current_gap)
query_results = []
for query_limit, required_size in placement_queries:
valid_obstacle_count = 0
# Binary search helps efficiently locate the relevant obstacles before the limit.
low_index = 0
high_index = len(sorted_obstacles) - 1
target_index = -1
while low_index <= high_index:
mid_index = (low_index + high_index) // 2
if sorted_obstacles[mid_index] < query_limit:
target_index = mid_index
low_index = mid_index + 1
else:
high_index = mid_index - 1
if target_index == -1:
can_fit = sorted_obstacles[0] >= required_size
else:
largest_gap_seen = max_gap_to_position[target_index]
initial_gap = sorted_obstacles[0]
can_fit = (largest_gap_seen >= required_size) or (initial_gap >= required_size and query_limit > sorted_obstacles[target_index] + required_size)
query_results.append(can_fit)
# Returning the boolean list gives immediate answers for all evaluated queries.
return query_results| Case | How to Handle |
|---|---|
| Querying for a block size larger than the maximum coordinate limit | The segment check naturally returns false because no gap can exceed the coordinate boundary. |
| Queries requesting placement or checks at coordinate zero | Segment tree or Fenwick tree indices must be offset or 1-indexed to safely handle zero coordinates without out-of-bounds errors. |
| Maximum possible coordinate values approaching the upper constraints | Coordinate compression or sparse segment tree implementations must be used to prevent memory limit exceed errors. |
| Consecutive duplicate obstacle insertions at the same coordinate | The data structure should ignore redundant insertions to maintain accurate contiguous gap measurements. |
| A query asking for a block size of one | The algorithm successfully validates it as long as there is at least one unit of free space before the query limit. |
| No obstacles placed yet when the first existence query is executed | The structure must treat the entire range up to the query limit as a single contiguous free space. |
| Interleaved placement and query operations covering the entire coordinate line | Using an ordered set combined with a segment tree ensures logarithmic time complexity per operation for efficient scaling. |
| Very large coordinate values causing potential integer overflow in segment calculations | 64-bit integer types must be used for all coordinate math and distance comparisons to prevent overflow bugs. |