Answer Lamp Illumination Queries on a Huge Grid and Switch Neighbors Off
Last updated: November 29, 2026
Quick Overview
On an n x n grid (n up to 10^9) with lamps at given cells, a lamp lights its row, column and both diagonals; for each query cell, report whether it is lit, then turn off any lamp in the query cell or its eight neighbors.
0
0
0 solved
On an n x n grid (n up to 10^9) with lamps at given cells, a lamp lights its row, column and both diagonals; for each query cell, report whether it is lit, then turn off any lamp in the query cell or its eight neighbors.
A Google onsite Hard problem where the grid is far too large to allocate. The interviewer checks that you represent illumination with counters keyed by row, column, diagonal and anti-diagonal, and that removal updates every counter consistently.
What the Interviewer Expects
- Recognizes the grid cannot be materialized
- Counts lamps per row, column, r - c diagonal and r + c anti-diagonal
- Answers lit status with four counter lookups
- Turns off up to nine lamps, decrementing all four counters
- Deduplicates repeated lamp positions
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Lamps can also be added between queries.
- Each lamp lights only up to distance d; how does that change things?
- Return how many lamps light each query cell.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
n,lamps(list of[r, c], may repeat),queries(list of[r, c]). - A cell is lit if any lamp shares its row, column, diagonal (
r - c) or anti-diagonal (r + c). - After answering...
High-Level Design
Store the set of active lamp positions plus four Counters: by row, column, r - c and r + c. A cell is lit if any of its four counters is positive. To switch off a neighbor lamp, remove it from t...