ORIGINAL CODING QUESTION
Recolour a desk zone
An office seating chart stores a team colour code in every cell. Repainting one cell also repaints every cell reachable from it through side-sharing neighbours that carry the same original colour. Return how many cells actually change colour; if the new colour equals the original colour, nothing changes and the answer is 0.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
rows cols, then rows×cols cell values in row-major order. Then startRow startCol newColour (zero-based start cell).
Constraints
1 ≤ rows,cols ≤ 8; 0 ≤ colour ≤ 9; the start cell lies inside the chart.
Example 1
Input
3 3 1 1 1 1 1 0 1 0 1 1 1 2
Output
6
The connected colour-1 zone has six cells; the bottom-right 1 touches it only diagonally.
Example 2
Input
2 2 3 3 3 3 0 0 3
Output
0
The new colour matches, so no cell changes.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Only cells connected to the start through equal original colours are repainted.
Hint 2
Run a breadth-first or depth-first search from the start cell over same-colour neighbours.
Hint 3
Handle the case where the new colour equals the old one before searching, then count visited cells.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →