Block Placement Queries - Problem

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:

For a query of type 1, 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.

For a query of type 2, 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 i-th query of type 2, and false otherwise.

Input & Output

Example 1 — Basic Operations
$ Input: queries = [[1,2],[2,3,3],[2,5,2]]
Output: [false,true]
💡 Note: Add obstacle at 2. Check if size-3 block fits in [0,3]: gap 0→2 is size 2, gap 2→3 is size 1, max gap < 3, so false. Check if size-2 block fits in [0,5]: gap 2→5 is size 3 ≥ 2, so true.
Example 2 — Multiple Obstacles
$ Input: queries = [[1,1],[1,4],[2,6,2],[2,6,3]]
Output: [true,false]
💡 Note: Add obstacles at 1 and 4. Check size-2 block in [0,6]: gaps are 0→1(1), 1→4(2), 4→6(2), max gap=2≥2, so true. Check size-3 block: max gap=2<3, so false.
Example 3 — No Obstacles
$ Input: queries = [[2,5,3],[1,2],[2,5,6]]
Output: [true,false]
💡 Note: Check size-3 block in [0,5] with no obstacles: entire range has size 6≥3, so true. Add obstacle at 2. Check size-6 block: max gap is now 3<6, so false.

Constraints

  • 1 ≤ queries.length ≤ 104
  • queries[i].length == 2 or 3
  • 1 ≤ x ≤ 5 × 104
  • 1 ≤ sz ≤ 5 × 104

Visualization

Tap to expand
Block Placement Queries Overviewqueries = [[1,2],[2,3,3],[2,5,2]]0123456Obstacle at 2Query: size=3 block in [0,3]✗ Max gap = 2 < 3Query: size=2 block in [0,5]✓ Gap 2→5 has size=3 ≥ 2Output: [false, true]
Understanding the Visualization
1
Input
Queries to add obstacles and check block placements
2
Process
Track obstacles and calculate available gaps
3
Output
Boolean array indicating if each block can be placed
Key Takeaway
🎯 Key Insight: Efficiently track gaps between obstacles to quickly determine block placement feasibility
Asked in
Google 35 Amazon 28 Microsoft 22
28.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen