KatInterview

Overlapping 2D Block Move with O(1) Extra Memory

Arrays & HashingAuto-graded · 26 test cases
MediumAuto-gradedArrays & Hashingarraysin-placetwo-dimensional

Last updated

Tests whether a candidate can copy a rectangular subregion of a 2D grid onto another position in the same grid, in place, without corrupting overlapping data.

You have a 2D grid of integers and two rectangular windows of the same size inside it. Your job is to overwrite the destination window with the values that the source window currently holds, then return the grid.

Both windows live in the same grid and may overlap. You must perform the operation in place: allocating a temporary copy of the block, any of its rows, or a second grid is not allowed. Only O(1) extra memory beyond the input is permitted.

Inputs

ParameterTypeMeaning
gridlist[list[int]]The 2D grid to modify
src_rowintTop row of the source window
src_colintLeft column of the source window
dst_rowintTop row of the destination window
dst_colintLeft column of the destination window
heightintNumber of rows in each window
widthintNumber of columns in each window

Rules

  • For every 0 <= i < height and 0 <= j < width, the cell (dst_row + i, dst_col + j) must end up holding the value that (src_row + i, src_col + j) held before the operation began, even if the two windows overlap.
  • Every cell that lies outside the destination window must keep its original value. This includes any source cells that the destination window does not cover.
  • Modify grid in place and return it (the same list object you received).
  • Slice assignment like row[a:b] = other[c:d] allocates a temporary list of length width on the right-hand side and counts as O(width) extra memory — do not use it.

Constraints

  • 1 <= len(grid) <= 500; 1 <= len(grid[0]) <= 500; every row has the same length.
  • -10^9 <= grid[r][c] <= 10^9
  • 1 <= height; 1 <= width
  • Both windows fit entirely inside the grid:
    • 0 <= src_row, src_row + height <= len(grid)
    • 0 <= dst_row, dst_row + height <= len(grid)
    • 0 <= src_col, src_col + width <= len(grid[0])
    • 0 <= dst_col, dst_col + width <= len(grid[0])
  • Extra memory must be O(1) beyond the input grid.

Examples

Example 1

Input: move_grid_block([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], 0, 0, 0, 1, 2, 3)

Output: [[1, 1, 2, 3], [5, 5, 6, 7], [9, 10, 11, 12]]

Example 2

Input: move_grid_block([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], 1, 1, 0, 0, 2, 2)

Output: [[6, 7, 3, 4], [10, 11, 7, 8], [9, 10, 11, 12]]

Example 3

Input: move_grid_block([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], 0, 0, 1, 2, 2, 2)

Output: [[1, 2, 3, 4], [5, 6, 1, 2], [9, 10, 5, 6]]

Hints

Hint 1

Think about what goes wrong when the windows overlap. If you copy top-to-bottom and left-to-right, can a destination write ever land on a source cell you haven't read yet? Construct a tiny example where that happens.

Hint 2

Flatten the 2D grid mentally into a single row-major array. Each source cell moves by the same flat offset delta = (dst_row - src_row) * W + (dst_col - src_col). Now you have the classic 1D overlapping-copy problem: when delta > 0 you must copy back-to-front; when delta < 0 you copy front-to-back. The sign of delta is just the lexicographic comparison of (dst_row, dst_col) with (src_row, src_col).

Hint 3

You don't need to compute delta explicitly. If (dst_row, dst_col) comes after (src_row, src_col) in row-major order — meaning dst_row > src_row, or same row and dst_col > src_col — iterate rows bottom-up and columns right-to-left. Otherwise iterate rows top-down and columns left-to-right. Convince yourself that no source cell can be overwritten before it is read under this rule.