Slide a Block in a Grid and Filter Repeated Log Calls
Data Structure DesignAuto-graded · 18 test casesLast updated
Move a rectangular block inside a 2D grid without corrupting overlapping data, then run a sliding-window duplicate filter over a list of timestamps.
You are given a square grid of integers and a list of timestamps. Solve two small tasks in order and return both results together.
Task 1: Slide a block
The rectangle of cells with top-left corner (top, left) and bottom-right corner (bottom, right) (both inclusive) is moved by (d_row, d_col). The cell at (r, c) in the rectangle is copied to (r + d_row, c + d_col).
- Every copied value is the value the cell held before the move began, even when the old and new rectangles overlap.
- Cells that the block leaves behind are not cleared. They keep their old values unless a moved cell lands on them.
- The grid is modified in place, and the modified grid is part of the returned result.
Task 2: Filter repeated log calls
Process times from left to right. Each entry is a log call whose message is identified by its own timestamp value. A call is accepted (True) if no accepted call with the same timestamp value is still inside the 10-second window. Otherwise it is rejected (False).
An earlier accepted call with timestamp p is still in the window for a call at time t when p >= t - 10. Rejected calls are not recorded. Your solution should keep memory proportional to the accepted calls still inside the window, and each call should take O(1) amortized time.
Output
Return a two-element list [grid, decisions]:
gridis the grid after the move.decisionsis a list of booleans, one per entry oftimes, in order.
Constraints
- The grid is
n x nwith1 <= n. 0 <= top <= bottom < nand0 <= left <= right < n.- The destination rectangle lies fully inside the grid.
d_rowandd_colmay be negative, zero, or positive. If both are zero, the grid is unchanged.timesmay be empty. Its values are integers in non-decreasing order.
Examples
Example 1
Input: relocate_block_and_filter_logs([[1, 2], [3, 4]], 0, 0, 0, 0, 1, 1, [1, 11, 11])
Output: [[[1, 2], [3, 1]], [True, True, False]]
Example 2
Input: relocate_block_and_filter_logs([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 0, 0, 1, 1, 1, 1, [0, 0, 5, 10, 11, 20])
Output: [[[1, 2, 3], [4, 1, 2], [7, 4, 5]], [True, False, True, True, True, True]]
Example 3
Input: relocate_block_and_filter_logs([[1, 2], [3, 4]], 0, 1, 1, 1, 0, -1, [1, 2, 2, 11])
Output: [[[2, 2], [4, 4]], [True, True, False, True]]
Example 4
Input: relocate_block_and_filter_logs([[5]], 0, 0, 0, 0, 0, 0, [])
Output: [[[5]], []]
Hints
Hint 1
If you move cells one by one directly, a write can destroy a source cell you have not read yet. Think about how to avoid that, either by copying the block first or by choosing the traversal direction from the signs of the shift.
Hint 2
For the log filter, keep a queue of accepted timestamps in arrival order plus a set for O(1) membership checks. Before each decision, drop entries from the front of the queue that have fallen out of the window.
Hint 3
Be careful with the window edge: an earlier accepted timestamp p stays in the window while p >= t - 10, so expire only entries strictly smaller than t - 10. Rejected calls must not be added to the queue.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The two tasks are independent, so we solve them one after the other.
Task 1: Slide a block
The danger is overlap. If the old and new rectangles intersect, writing a cell directly can overwrite a source cell that has not been read yet.
The simplest fix is to copy the block into a temporary list of rows first. Then every value is read from the snapshot, so writes cannot affect later reads. Every snapshot value is written to its shifted position. Cells that are not overwritten keep their old contents, which gives the required "vacated cells keep their values" behavior.
An O(1) extra-space alternative is to iterate rows bottom to top when d_row > 0 (top to bottom otherwise), and columns right to left when d_col > 0 (left to right otherwise). That order always reads a cell before anything overwrites it. The snapshot version is simpler and is what the reference uses.
Task 2: Filter repeated log calls
Keep two structures:
- A deque of accepted timestamps in arrival order.
- A set of the same timestamps for O(1) membership tests.
For each timestamp t:
- Compute
cutoff = t - 10. While the front of the deque is smaller thancutoff, pop it and remove it from the set. These entries are outside the window. - If
tis in the set, an identical accepted call is still in the window, so the answer isFalse. - Otherwise add
tto both structures and answerTrue.
Rejected calls are never stored, so memory stays proportional to the accepted calls inside the window.
Why it is correct
After the cleanup step, the set holds exactly the accepted timestamps p with p >= t - 10, because timestamps are non-decreasing and the deque is ordered. The membership test therefore checks exactly the rule in the statement. Each timestamp is pushed and popped at most once, so the work is amortized O(1) per call.
Complexity
- Task 1: time O(h * w) for a block of h rows and w columns, with O(h * w) extra space for the snapshot.
- Task 2: time O(m) for
mtimestamps, amortized O(1) each. Space is O(k), where k is the number of accepted timestamps inside the window.
Reference solution
from collections import deque
from typing import List
def relocate_block_and_filter_logs(grid: List[List[int]], top: int, left: int, bottom: int, right: int, d_row: int, d_col: int, times: List[int]) -> List:
# Task 1: snapshot the block, then write it at the shifted position.
block = [row[left:right + 1] for row in grid[top:bottom + 1]]
for i, row in enumerate(block):
for j, value in enumerate(row):
grid[top + d_row + i][left + d_col + j] = value
# Task 2: sliding-window duplicate filter.
window = deque()
in_window = set()
decisions = []
for t in times:
cutoff = t - 10
while window and window[0] < cutoff:
old = window.popleft()
in_window.remove(old)
if t in in_window:
decisions.append(False)
else:
in_window.add(t)
window.append(t)
decisions.append(True)
return [grid, decisions]