KatInterview

Square Windows Containing Each Marked Cell

Arrays & HashingAuto-graded · 18 test cases
EasyAuto-gradedArrays & Hashinggridhash-mapcoordinates

Last updated

Map each marked grid cell to the sorted top-left corners of all in-bounds 2x2 windows that contain it, testing coordinate arithmetic and bounds handling.

Square Windows Containing Each Marked Cell

A grid has R rows and C columns, with rows indexed from 0 to R-1 and columns from 0 to C-1. You receive a list of marked cells, each given as [r, c].

A window is a 2x2 square of cells identified by its top-left corner (tr, tc). It occupies the cells (tr, tc), (tr, tc+1), (tr+1, tc) and (tr+1, tc+1). A window is valid only if it lies completely inside the grid, which means 0 <= tr <= R-2 and 0 <= tc <= C-2.

A window contains a marked cell if that cell is one of the four cells the window occupies.

Task

Implement a function that returns a dictionary describing, for every marked cell, which valid windows contain it.

  • Each key is a string of the form "r,c" (the cell's row and column joined by a comma, with no spaces), for example "1,1".
  • Each value is a list of [tr, tc] pairs, one for every valid window that contains that cell.
  • Each value list must be sorted in ascending lexicographic order: by tr first, then by tc.

Rules and edge cases

  • A marked cell always gets a key, even if no valid window contains it. In that case its value is an empty list [].
  • If the grid is too small to hold any 2x2 window (R < 2 or C < 2), every value is [].
  • A marked cell lying outside the grid is not rejected. It still receives a key, and its list contains only those candidate windows that satisfy the validity condition (often this is empty).
  • If the same cell appears more than once, it produces the same key, so the dictionary holds a single entry for it.
  • If the list of marked cells is empty, return {}.

Constraints

  • 0 <= len(marked_cells); the list may be large relative to nothing else, so the running time should not depend on R * C.
  • Each marked cell is a list of two integers [r, c].
  • R and C are integers and may be small (including less than 2).
  • The output must use string keys and lists of [tr, tc] lists exactly as described.

Examples

Example 1

Input: get_windows_for_cells([[0, 0]], 3, 3)

Output: {'0,0': [[0, 0]]}

Example 2

Input: get_windows_for_cells([[1, 1]], 3, 3)

Output: {'1,1': [[0, 0], [0, 1], [1, 0], [1, 1]]}

Hints

Hint 1

A window with top-left corner (tr, tc) occupies rows tr and tr+1 and columns tc and tc+1. Turn this around: which values of tr can make row r part of a window?

Hint 2

Only tr = r-1 or tr = r can contain row r, and only tc = c-1 or tc = c can contain column c. That gives at most four candidate windows per marked cell, so there is no need to scan the grid.

Hint 3

Keep a candidate only if 0 <= tr <= R-2 and 0 <= tc <= C-2. Iterate tr and tc in increasing order (or sort at the end) so each list is lexicographically sorted, and build the key as the string f"{r},{c}".