Black Dots

Solution

You may be able to find the solution on the discussion forum.

2026 Update: Gestalt Perception and Counting Algorithms

Visual perception puzzles (how many dots? how many triangles?) test systematic enumeration — a key skill in algorithm design and combinatorics.

from itertools import combinations

def count_collinear_sets(points: list, k: int = 3) -> int:
    """
    Count sets of k collinear points.
    Uses slope comparison — O(n^3) for k=3.
    """
    def are_collinear(p1, p2, p3):
        x1, y1 = p1; x2, y2 = p2; x3, y3 = p3
        # Use cross product (avoids floating point): (p2-p1) x (p3-p1) = 0
        return (y2-y1)*(x3-x1) == (y3-y1)*(x2-x1)

    count = 0
    for combo in combinations(points, k):
        if k == 3 and are_collinear(*combo):
            count += 1
    return count

def count_dots_in_grid(rows: int, cols: int) -> int:
    """Count dots (intersection points) in an m×n grid."""
    return rows * cols  # Trivial: each cell has one dot at its corner... actually grid has (r+1)*(c+1) intersections

def grid_intersection_count(rows: int, cols: int) -> int:
    """Count intersection points in a grid with 'rows' horizontal and 'cols' vertical lines."""
    return (rows + 1) * (cols + 1)  # Each pair of perpendicular lines creates an intersection

# Pattern recognition problem: connected components (flood fill)
def count_connected_blobs(grid: list) -> int:
    """
    Count connected black dots (blobs) in a binary grid.
    Use BFS/DFS flood fill.
    """
    rows, cols = len(grid), len(grid[0])
    visited = set()
    blobs = 0

    def bfs(r, c):
        queue = [(r, c)]
        while queue:
            row, col = queue.pop()
            for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
                nr, nc = row+dr, col+dc
                if (0 <= nr < rows and 0 <= nc < cols and
                    grid[nr][nc] == 1 and (nr, nc) not in visited):
                    visited.add((nr, nc))
                    queue.append((nr, nc))

    for r in range(rows):
        for c in range(cols):
            if grid[r][c] == 1 and (r, c) not in visited:
                visited.add((r, c))
                bfs(r, c)
                blobs += 1

    return blobs

# Test: count blobs in a grid (1=black dot, 0=white)
grid = [
    [1, 1, 0, 0, 0],
    [1, 0, 0, 1, 1],
    [0, 0, 0, 0, 0],
    [0, 1, 0, 0, 1],
]
print(f"Grid has {count_connected_blobs(grid)} blobs")  # 4

# Interview applications:
# - LeetCode #200 (Number of Islands) — same flood fill
# - LeetCode #695 (Max Area of Island) — find largest blob
# - LeetCode #547 (Number of Provinces) — connected components in graph

Interview insight: “Counting dots/shapes in figures” tests systematic enumeration over brute force visual counting. The underlying algorithm — connected components via BFS/DFS — appears as: counting islands, counting provinces (union-find), detecting blobs in computer vision, and identifying communities in social graphs. LeetCode #200 is the canonical coding interview version.

Scroll to Top