The Grid Illumination interview question takes place on an N x N grid. You are given a list of lamps, each illuminating its own cell, its entire row, its entire column, and its two diagonals. You are also given a list of queries. For each query (r, c), you must check if the cell is illuminated. After answering a query, you must turn off the lamp at (r, c) and any lamps in the adjacent 8 cells.
Dropbox and other tech companies ask this Hash Table coding problem to test a candidate's ability to represent grid properties algebraically. Since N can be up to , an matrix is impossible. It evaluates if you know how to use Hash Maps to count "lines of illumination" (rows, columns, and diagonals) and how to handle dynamic updates (turning off lamps) efficiently.
This problem relies on Algebraic Grid Representation and Hash Maps.
(x, y) contributes to:
xyx - yx + y(r, c) is illuminated if row[r] > 0, col[c] > 0, diag1[r - c] > 0, or diag2[r + c] > 0.(r, c). If a lamp exists there in your active lamp Set, remove it and decrement the counts in your four Hash Maps.N = 5, Lamp at (1, 1).
row[1]=1, col[1]=1, d1[0]=1 (1-1), d2[2]=1 (1+1).(2, 2): Check row[2], col[2]. Check d1[2-2=0]. d1[0] is 1, so it is illuminated!(2, 2): Checks (1, 1), (1, 2) .... Finds lamp at (1, 1). Removes it. Decrements all maps.x - y and x + y are standard constants for cells on the same diagonal.Memorize the diagonal formulas for grid problems. If you need to group cells that share a diagonal going down-right, use row - col. For diagonals going down-left, use row + col. This trick appears in N-Queens, Grid Illumination, and many other matrix problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| First Missing Positive | Hard | Solve | |
| Maximum Equal Frequency | Hard | Solve | |
| 4Sum II | Medium | Solve | |
| Assign Elements to Groups with Constraints | Medium | Solve | |
| Brick Wall | Medium | Solve |