Compute board-game score from regions
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
You are given a 2D board for a board game. Each cell is encoded like `G1` or `W0`:
- The first character is the **terrain/area type** (e.g., `G`, `W`, `S`).
- The digit is the number of **crowns** in that cell (0–9).
Two cells belong to the same **area/region** if they have the same terrain type and are connected by 4-directional adjacency (up/down/left/right).
For each region:
\[
\text{regionScore} = (\#\text{cells in region}) \times (\text{sum of crowns in region})
\]
Return the **total score** across all regions.
### Example
Board (rows):
1. `G1, G2, W0, W1, S1`
2. `G2, G3, W0, W1, S1`
3. `S2, S3, S1, G1, S1`
4. `G1, G2, W0, W1, S1`
5. `G1, G2, W0, W1, S1`
Compute the total score for this board.
### Additional requirement
Write a small set of self-contained test cases that cover edge cases (e.g., all same terrain, all crowns 0, single-cell regions, multiple disjoint regions of same terrain type).
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.
You are given a rectangular 2D board for a board game. Each cell is encoded as a string like G1 or W0. The first character is the terrain type, and the digit(s) after it represent the number of crowns in that cell.
Two cells belong to the same region if they have the same terrain type and are connected using 4-directional adjacency only: up, down, left, or right. Diagonal cells do not connect.
For each region, compute:
regionScore = (number of cells in the region) * (sum of crowns in the region)
Return the total score across all regions. If the board is empty, return 0.
Example board:
[
['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']
]
The correct total score for this board is 116.
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.