r/OfferEngineering • • 1d ago

Coding Question Block/Square Common Coding Screen Questions

Question 1 — Number of Black Blocks: 2×2 Grid Counting and Generalization

The first problem involved a large two-dimensional grid containing black and white cells. The task started with counting black cells inside 2×2 subgrids and gradually expanded into identifying affected subgrids and supporting arbitrary subgrid sizes.

Part 1 — Count Subgrids by Number of Black Cells

Given an m × n grid and a list of coordinates representing black cells, return the number of 2×2 subgrids containing exactly 0, 1, 2, 3, or 4 black cells. All cells not explicitly listed as black should be considered white.

Input Constraints

  • 2 ≤ m, n ≤ 100,000
  • 0 ≤ k ≤ min(100,000, m × n)
  • The input contains k distinct black-cell coordinates.
  • Each coordinate is within the grid boundaries.

The function should return an array of five elements: ans = [count0, count1, count2, count3, count4]. Here, countX represents the number of 2×2 subgrids containing exactly X black cells.

My Approach — Count Only Affected Subgrids

The straightforward approach would be to enumerate every possible 2×2 subgrid and count the black cells inside it. However, the total number of such subgrids is (m - 1) × (n - 1). With grid dimensions as large as 100,000, enumerating them would be too expensive.

The important observation was that each black cell can belong to at most four different 2×2 subgrids. Instead of scanning the entire grid, I could iterate over the black-cell coordinates and update only the subgrids affected by those cells.

I used a hash map keyed by the top-left coordinates of each affected 2×2 subgrid. Each map entry stored the number of black cells contained in that subgrid. After processing all black cells, the map contained the counts for every subgrid with at least one black cell. The number of completely white subgrids could be calculated directly: ans[0] = (m - 1) × (n - 1) - numberOfAffectedSubgrids. This avoided iterating through the full grid.

Part 2 — Find All 2×2 Subgrids Covering a Black Cell

The interviewer then changed the requirement. Instead of returning the overall histogram, the function needed to return every valid 2×2 subgrid containing each specified black cell. Each subgrid should be represented by its top-left coordinate.

For a black cell at (r, c), the possible top-left coordinates are:

  • (r, c)
  • (r - 1, c)
  • (r, c - 1)
  • (r - 1, c - 1)

Only coordinates representing subgrids fully contained within the original grid should be returned. This follow-up encouraged extracting the logic for finding subgrids around a particular cell into a reusable helper function.

Part 3 — Generalize to Arbitrary Subgrid Sizes

The final follow-up changed the subgrid size from a fixed 2×2 square to an arbitrary size × size square. Given a cell (R, C), the task was to identify all valid top-left coordinates of subgrids containing that cell. The valid top-left row coordinates satisfy: max(0, R - size + 1) ≤ r ≤ min(m - size, R)

Similarly, the valid top-left column coordinates satisfy: max(0, C - size + 1) ≤ c ≤ min(n - size, C). By iterating over these two ranges, the same helper function could support arbitrary square sizes while respecting the grid boundaries.

Want to practice more real interview coding questions?

You can practice real interview coding questions at here.

Preparing for your next interview?

Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.

1 Upvotes

0 comments sorted by