Compute board-game score from regions
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates grid traversal and connected-component identification skills along with region-based aggregation (counting cells and summing crowns) in the Coding & Algorithms category.
Constraints
- 0 <= number of rows, number of columns <= 200
- The board is rectangular
- Each cell contains a terrain character followed by a crown count from 0 to 9
- Cells are connected only in 4 directions, not diagonally
Examples
Input: [['G1', 'G2', 'W0', 'W1', 'S1'], ['G2', 'G3', 'W0', 'W1', 'S1'], ['S2', 'S3', 'S1', 'G1', 'S1'], ['G1', 'G2', 'W0', 'W1', 'S1'], ['G1', 'G2', 'W0', 'W1', 'S1']]
Expected Output: 116
Explanation: Regions are scored separately: top-left G=32, top W=8, middle-left S=18, right-column S=25, center G=1, bottom-left G=24, bottom W=8. Total = 116.
Input: []
Expected Output: 0
Explanation: An empty board has no regions, so the total score is 0.
Input: [['G1', 'G2'], ['G0', 'G3']]
Expected Output: 24
Explanation: All four cells are one G region. Size = 4, crowns = 1+2+0+3 = 6, so score = 4*6 = 24.
Input: [['S0', 'W0'], ['W0', 'S0']]
Expected Output: 0
Explanation: All cells have 0 crowns. Every region contributes 0 regardless of its size.
Input: [['G1', 'W0', 'G2'], ['W0', 'W0', 'W0'], ['G3', 'W0', 'G4']]
Expected Output: 10
Explanation: The four G cells are four separate single-cell regions scoring 1, 2, 3, and 4. The W region has 0 total crowns, so it contributes 0. Total = 10.
Hints
- This is a connected-components problem on a grid. Use DFS or BFS to explore one region at a time.
- While traversing a region, accumulate both the number of cells and the total crowns, then add their product to the answer.