DSA interview question
A puzzle board has stones on integer grid points. A stone can be removed if another remaining stone shares its row or its column. What's the maximum number of stones that can be removed? Stones that share a row or column are linked, and linking is transitive, so the board splits into connected groups. Each group can be removed down to a single stone (remove them in reverse DFS order from a leaf), but never to zero. So the answer is stones − number of groups, and union-find computes the groups by linking each stone to the first stone seen in its row and in its column.
Example: [[0,0], [0,1], [1,0], [1,2], [2,1], [2,2]] → 5.
int removeStones(const vector<pair<int, int>>& stones); // distinct positions
| Input (stones) | Output | Explanation |
|---|---|---|
[[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]] | 5 | One group of 6 |
[[0,0],[0,2],[1,1],[2,0],[2,2]] | 3 | The groups have 4 and 1 stones |
[[0,0],[0,1],[0,2]] | 2 | One row |
[[0,0],[1,1]] | 0 | Nothing is shared |
[[0,0]] | 0 |
(Confirm: positions are distinct; a stone needs a remaining partner in its row or column at the moment it's removed.)
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