DSA interview question
A solar-farm planner has a grid map where 1 is usable flat land and 0 is an obstacle, and wants the largest rectangular panel array that sits entirely on usable land. Checking every rectangle is O(R²C²). The trick is to reduce it to the histogram problem: process the grid row by row, and for each column keep the number of consecutive 1s ending at the current row. That array is a histogram whose bars stand on the current row, and the largest rectangle in it is the best rectangle whose bottom edge is this row.
Example: rows 10100 / 10111 / 11111 / 10010 → 6.
int maximalRectangle(const vector<string>& matrix); // characters '0' and '1'
| Input (rows) | Output | Explanation |
|---|---|---|
10100 / 10111 / 11111 / 10010 | 6 | Rows 2–3, columns 2–4 (0-based) |
0 | 0 | |
1 | 1 | |
11 / 11 | 4 | |
101 / 111 / 111 | 6 | The bottom two rows |
(Confirm: the rectangle must contain only 1s and be axis-aligned; the matrix may be empty.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in