Count Regions in a Grid Divided by Slash Characters

Last updated: November 29, 2026

Quick Overview

An n by n grid of unit squares contains '/', '\', or ' ' in each cell, where slashes are diagonal walls. Return the number of separate regions the walls divide the grid into.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


An n by n grid of unit squares contains '/', '\', or ' ' in each cell, where slashes are diagonal walls. Return the number of separate regions the walls divide the grid into.

A Meta onsite question that tests modeling skill more than recall, framed around a whiteboard or sticker drawing feature that must detect enclosed areas. The interviewer checks that you split each cell into four triangles, union them according to the character, and union across cell borders.

What the Interviewer Expects
  • Split each cell into four triangles (top, right, bottom, left)
  • Union triangles inside a cell based on the character
  • Union neighbors across shared edges with adjacent cells
  • Count components at the end
  • Explain why a pixel upscaling approach also works but costs more
Key Topics to Cover
union find
grid modeling
connected components
geometry
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you solve it by upscaling each cell to a 3x3 pixel block and flood filling?
  • How would you report the area of each region?
  • What if walls could also be horizontal or vertical half segments?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: grid, a list of n strings of length n with characters /, \, or space.
  • Output: number of regions....
High-Level Design

Divide each cell by both diagonals into four triangles: 0 top, 1 right, 2 bottom, 3 left. Index triangle t of cell (r, c) as 4 * (r * n + c) + t.

Inside the cell.

  • Space: union all...

Submit Your Answer
Markdown supported