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 INPUT Number Line (0 to infinity) 0 1 2 3 4 Queries Array: Query 1: [1, 2] Type 1: Build obstacle at x=2 Query 2: [2, 3, 3] Type 2: Place block sz=3 in [0,3] Query 3: [2, 5, 2] Type 2: Place block sz=2 in [0,5] Query Types: Type 1: Add obstacle Type 2: Check block fit ALGORITHM STEPS 1 Process [1, 2] Add obstacle at x=2 Gaps: [0,2], [2,inf) 2 Process [2, 3, 3] Need gap of size 3 in [0,3] Max gap in [0,3] = 2 3 Result: false 2 < 3, cannot fit block 4 Process [2, 5, 2] Need gap of size 2 in [0,5] Gap [2,5] has size 3 >= 2 Sorted List Gap Tracking 2 Gap: 2 Gap: 3+ 0 5 FINAL RESULT Query Results: Query [2, 3, 3] Block size 3 in range [0,3] false Query [2, 5, 2] Block size 2 in range [0,5] true Output Array: false true Result: [false, true] 2 type-2 queries answered Key Insight: Use a sorted list to track obstacle positions. For each type-2 query, find the maximum gap between consecutive obstacles within the query range [0, x]. A block of size sz fits only if there exists a gap >= sz. Binary search enables O(log n) obstacle insertion and gap queries. TutorialsPoint - Block Placement Queries | Sorted List with Gap Tracking
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