KatInterview

Slide a Block in a Grid and Filter Repeated Log Calls

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designmatrixin-placesliding-window

Last 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]:

  • grid is the grid after the move.
  • decisions is a list of booleans, one per entry of times, in order.

Constraints

  • The grid is n x n with 1 <= n.
  • 0 <= top <= bottom < n and 0 <= left <= right < n.
  • The destination rectangle lies fully inside the grid.
  • d_row and d_col may be negative, zero, or positive. If both are zero, the grid is unchanged.
  • times may 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.