← 返回博客列表
Snap

Snap Geometric Algorithm Interview: Building Perimeter

2025-08-11

Snap interview geometric algorithm question. This article demonstrates how to handle geometric problems on a 2D grid through the building perimeter calculation problem. oavoservice helps you master core geometric algorithm techniques.

📋 Problem Redefinition

Given a 2D grid where 1 represents a building and 0 represents empty space. Buildings may connect to form irregular shapes. Calculate the perimeter of the outer boundary of all buildings (only count edges touching empty space or the boundary).

Example:

Input: grid = [
  [1,1,0],
  [1,1,0],
  [0,0,1]
]
Output: 12

🎯 Core Idea

  1. Boundary Identification - Find all edges touching empty space
  2. Deduplication - Avoid double counting shared edges
  3. Edge Cases - Handle grid boundaries
  4. Optimization - Reduce unnecessary traversals

💡 Solution Method (oavoservice Innovative Approach)

Method 1: Direct Counting

def calculate_perimeter(grid):
    if not grid or not grid[0]:
        return 0
    
    rows, cols = len(grid), len(grid[0])
    perimeter = 0
    
    for i in range(rows):
        for j in range(cols):
            if grid[i][j] == 1:
                # Check 4 directions
                if i == 0 or grid[i-1][j] == 0:
                    perimeter += 1
                if i == rows-1 or grid[i+1][j] == 0:
                    perimeter += 1
                if j == 0 or grid[i][j-1] == 0:
                    perimeter += 1
                if j == cols-1 or grid[i][j+1] == 0:
                    perimeter += 1
    
    return perimeter

Time Complexity: O(m × n) Space Complexity: O(1)

Method 2: Mathematical Optimization

oavoservice optimization strategy:

def calculate_perimeter_optimized(grid):
    rows, cols = len(grid), len(grid[0])
    buildings = 0
    shared_edges = 0
    
    for i in range(rows):
        for j in range(cols):
            if grid[i][j] == 1:
                buildings += 1
                
                # Check right and bottom neighbors for shared edges
                if j < cols - 1 and grid[i][j+1] == 1:
                    shared_edges += 1
                if i < rows - 1 and grid[i+1][j] == 1:
                    shared_edges += 1
    
    # Each building contributes 4 edges, subtract 2 for each shared edge
    return buildings * 4 - shared_edges * 2

🚀 Extension Problems

1. 3D Surface Area

def calculate_surface_area_3d(grid):
    """
    grid[i][j] represents height of building at that position
    """
    if not grid:
        return 0
    
    rows, cols = len(grid), len(grid[0])
    surface_area = 0
    
    for i in range(rows):
        for j in range(cols):
            if grid[i][j] > 0:
                # Top and Bottom
                surface_area += 2
                
                # 4 Sides
                for di, dj in [(0,1), (0,-1), (1,0), (-1,0)]:
                    ni, nj = i + di, j + dj
                    neighbor_height = 0
                    
                    if 0 <= ni < rows and 0 <= nj < cols:
                        neighbor_height = grid[ni][nj]
                    
                    # Only the part higher than neighbor contributes to surface area
                    if grid[i][j] > neighbor_height:
                        surface_area += grid[i][j] - neighbor_height
    
    return surface_area

💼 How oavoservice Helps

Geometric Thinking - Transforming problems into geometric calculations Optimization Strategy - Mathematical methods to reduce computation Extension Ability - 3D and complex scenarios Code Implementation - Clear logic structure

Contact oavoservice for professional geometric algorithm interview assistance!


Tags: #Snap #GeometricAlgorithm #GridTraversal #VOHelp #InterviewPrep #1point3acres


Need real interview questions? Contact WeChat Coding0201 immediately to get real questions.