Build a QuadTree From a Grid
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Implement a compressed QuadTree for a square grid.
You are given an `n x n` grid of values, where `n` is a power of two. The values are not limited to binary values; they may be any comparable type such as integers or strings.
Define a QuadTree node with:
- `value`: the value stored for a leaf region. For non-leaf nodes, this may be any placeholder value.
- `isLeaf`: whether this node represents a uniform region.
- `topLeft`, `topRight`, `bottomLeft`, `bottomRight`: child pointers, all `null` for a leaf node.
Build and return the root of a compressed QuadTree using the following rules:
1. A region is represented by a leaf node if every cell in that region has the same value.
2. Otherwise, split the region into four equal quadrants and recursively build child nodes for each quadrant.
3. The root represents the entire grid.
Example:
Input:
```text
[
[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4]
]
```
Output: an internal root node with four leaf children, whose values are `1`, `2`, `3`, and `4` for the top-left, top-right, bottom-left, and bottom-right quadrants respectively.
Implement the data structure and the `buildQuadTree(grid)` function.
### Binary Node Variant
Implement `Solution.construct(grid)` and return a node reference for the binary-grid variant. The C++ signature is `Node* construct(vector<vector<int>>& grid)`.
Here `n == grid.length == grid[i].length`, `n = 2^x` with `0 <= x <= 6`, and every cell is `0` or `1`. Nodes expose Boolean fields `val` and `isLeaf` and the four named child pointers. A uniform region must be a leaf with its Boolean value and four null children. A nonuniform region must be internal with all four child regions in top-left, top-right, bottom-left, bottom-right order. An internal node's `val` may be either `true` or `false`; both are valid. Return the compressed tree for the whole grid.
The quadrant layout is:
```text
topLeft | topRight
------------+------------
bottomLeft | bottomRight
```
For `[[0,1],[1,0]]`, the root is internal and its four children are leaves with values `false, true, true, false`, respectively. One level-order illustration is `[[0,1],[1,0],[1,1],[1,1],[1,0]]`, where each pair is `[isLeaf,val]` and Boolean values are written as `0` or `1`. Choosing `0` instead of `1` for the root's value is also valid.
For this larger example:
```text
[
[1,1,1,1,0,0,0,0],
[1,1,1,1,0,0,0,0],
[1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1],
[1,1,1,1,0,0,0,0],
[1,1,1,1,0,0,0,0],
[1,1,1,1,0,0,0,0],
[1,1,1,1,0,0,0,0]
]
```
The root's top-left and bottom-left children are `true` leaves, and its bottom-right child is a `false` leaf. Its top-right child is internal, with leaf children `false, false, true, true` in quadrant order. One level-order illustration is `[[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]`. The null entries mark absent children below a leaf; trailing absent entries are omitted. This serialization illustrates the tree and is not the function's return type.
### Existing General-Value Console
The original `buildQuadTree(grid)` task accepts equality-comparable values, including strings; its node field is named `value`, and internal values are placeholders. It has no stated size cap. Equality is the criterion for a uniform region; numeric addition and ordering are not required.
The existing console is a separate adapter named `solution(grid)`. It accepts a nonempty square grid with power-of-two `n <= 64` and equality-comparable values. Return a nested dictionary with exactly `value`, `isLeaf`, `topLeft`, `topRight`, `bottomLeft`, and `bottomRight` at each node. A leaf retains its original value and has four `None` children. An internal dictionary has `value = None`, `isLeaf = False`, and four child dictionaries. Its deterministic `None` placeholder does not restrict the Boolean internal-value freedom of the binary node variant.
### What a Strong Answer Covers
- The correct value and extent of every leaf, with uniform regions represented without unnecessary children.
- Correct quadrant positions and complete coverage of the input grid.
- The field names, null children, and internal-value policy for each interface.
- Time and storage bounds that distinguish the grid, working memory, and returned tree.
Overview: This question evaluates understanding of spatial data structures and recursive divide-and-conquer techniques by compressing uniform regions into a QuadTree representation.
Read the full Uber Software Engineer interview experience this question came from
Given a non-empty `n x n` grid, where `n` is a power of two, build a compressed QuadTree for the whole grid.
A region should be represented by a leaf node if every cell in that region has the same value. Otherwise, split the region into four equal quadrants and recursively build child nodes in this order: top-left, top-right, bottom-left, bottom-right.
For deterministic judging, return the tree as a nested Python dictionary with exactly these keys: `value`, `isLeaf`, `topLeft`, `topRight`, `bottomLeft`, `bottomRight`.
Rules for the returned structure:
- Leaf node: `isLeaf` is `True`, `value` is the region's value, and all four child fields are `None`.
- Internal node: `isLeaf` is `False`, `value` must be `None`, and all four child fields contain child nodes.
You may implement a custom `Node` class internally if you want, but the final return value from `solution(grid)` must follow the nested dictionary format above.
Constraints
- 1 <= n <= 64
- n is a power of two
- Each row of `grid` has length `n`
- Values support equality comparison
Examples
Input: ([[1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]],)
Expected Output: {'value': None, 'isLeaf': False, 'topLeft': {'value': 1, 'isLeaf': True, 'topLeft': None, 'topRight': None, 'bottomLeft': None, 'bottomRight': None}, 'topRight': {'value': 2, 'isLeaf': True, 'topLeft': None, 'topRight': None, 'bottomLeft': None, 'bottomRight': None}, 'bottomLeft': {'value': 3, 'isLeaf': True, 'topLeft': None, 'topRight': None, 'bottomLeft': None, 'bottomRight': None}, 'bottomRight': {'value': 4, 'isLeaf': True, 'topLeft': None, 'topRight': None, 'bottomLeft': None, 'bottomRight': None}}
Explanation: Each 2 x 2 quadrant is uniform, so the root is an internal node with four leaf children holding values 1, 2, 3, and 4.
Input: ([[7, 7], [7, 7]],)
Expected Output: {'value': 7, 'isLeaf': True, 'topLeft': None, 'topRight': None, 'bottomLeft': None, 'bottomRight': None}
Explanation: The entire grid has the same value, so it compresses into a single leaf node.
Hints
- Use divide-and-conquer recursion on square sub-regions of the grid.
- Before splitting a region, scan it to check whether all cells are equal; as soon as you find a different value, you can stop and split.