Skip to content

ORIGINAL CODING QUESTION

Desks cut off from every exit

An office map marks staffed desks with 1 and walkways with 0. Desks link only through shared sides. A desk group touching any outer edge of the map has a way out. Count the desk cells that belong to groups with no cell on the outer edge.

Open practice workspace

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.

Constraints

1 ≤ rows,cols ≤ 8; each cell is 0 or 1.

Example 1

Input

4 4 0 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0

Output

3

The three-desk group in the middle never touches an edge; the corner desk does.

Example 2

Input

3 3 1 1 1 1 1 1 1 1 1

Output

0

Every desk connects to the edge.

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

Instead of testing each desk, think about which desks can reach the edge.

Hint 2

Start a search from every desk on the outer edge and mark everything it reaches.

Hint 3

Desks left unmarked after those searches are the enclosed ones.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →