Design Neighbor Sum Service - Problem
Design a Neighbor Sum Service that efficiently calculates sums of adjacent and diagonal neighbors in a 2D grid.

You're given an n × n 2D grid containing distinct elements in the range [0, n² - 1]. Your task is to implement the NeighborSum class with the following methods:

Constructor: NeighborSum(int[][] grid) - Initialize the object with the grid
Adjacent Sum: int adjacentSum(int value) - Return sum of elements adjacent to value (top, bottom, left, right)
Diagonal Sum: int diagonalSum(int value) - Return sum of elements diagonal to value (top-left, top-right, bottom-left, bottom-right)

Example: For a 3×3 grid with value 5 at position (1,1), adjacent neighbors would be the cells directly above, below, left, and right of position (1,1).

Input & Output

example_1.py — Basic 3x3 Grid
$ Input: grid = [[0,1,2],[3,4,5],[6,7,8]] neighborSum = NeighborSum(grid) neighborSum.adjacentSum(1) neighborSum.adjacentSum(4) neighborSum.diagonalSum(4) neighborSum.diagonalSum(8)
Output: [null, 6, 16, 16, 4]
💡 Note: For adjacentSum(1): neighbors are 0 and 4, sum = 6. For adjacentSum(4): neighbors are 1,3,5,7, sum = 16. For diagonalSum(4): neighbors are 0,2,6,8, sum = 16. For diagonalSum(8): only neighbor is 4, sum = 4.
example_2.py — Edge Cases
$ Input: grid = [[1,2],[3,4]] neighborSum = NeighborSum(grid) neighborSum.adjacentSum(1) neighborSum.diagonalSum(1) neighborSum.adjacentSum(2) neighborSum.diagonalSum(4)
Output: [null, 5, 4, 4, 1]
💡 Note: Corner and edge elements have fewer neighbors. For adjacentSum(1): neighbors are 2,3, sum = 5. For diagonalSum(1): only diagonal neighbor is 4, sum = 4.
example_3.py — Single Element
$ Input: grid = [[5]] neighborSum = NeighborSum(grid) neighborSum.adjacentSum(5) neighborSum.diagonalSum(5)
Output: [null, 0, 0]
💡 Note: Single element grid has no neighbors, so both sums return 0.

Visualization

Tap to expand
Neighbor Sum Service - Smart Home SecurityNeighborhood Grid123456789Adjacent: 2+4+6+8 = 20Diagonal Neighbors123456789Diagonal: 1+3+7+9 = 20Precomputed Lookup TableHouse 5 Alert Triggered!• Adjacent Sum: 20 (instant lookup)• Diagonal Sum: 20 (instant lookup)• Total Security Score: 40⚡ O(1) response time thanks to precomputation!
Understanding the Visualization
1
Grid Setup
Each cell represents a house with a unique ID, positioned in a neighborhood grid
2
Neighbor Types
Adjacent neighbors share walls (up/down/left/right), diagonal neighbors are at corners
3
Precomputation
During setup, calculate and store neighbor sums for each house ID
4
Instant Queries
When an alert occurs, instantly return the precomputed neighbor sum
Key Takeaway
🎯 Key Insight: Precomputing neighbor sums during initialization enables instant O(1) query responses, making this approach perfect for systems requiring fast, repeated lookups.

Time & Space Complexity

Time Complexity
⏱️
O(n²) initialization, O(1) per query

Process each cell once during init, constant lookup for queries

n
2n
Quadratic Growth
Space Complexity
O(n²)

Two hash maps storing sums for all n² elements

n
2n
Quadratic Space

Constraints

  • 3 ≤ n ≤ 10
  • 0 ≤ grid[i][j] ≤ n2 - 1
  • All grid[i][j] are distinct
  • 1 ≤ value ≤ n2 - 1
  • At most 2 × 104 calls will be made to adjacentSum and diagonalSum
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 6
23.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen