Traverse a tree and answer 2D prefix sums
Problem Description
You are presented with two independent coding tasks.
Task 1: In-order Traversal of a Binary Tree
Given the root of a binary tree, return an array or list containing the node values in in-order traversal.
- In-order traversal definition: Process the left subtree, then visit the current node, then process the right subtree.
- Input:
rootof the binary tree (may benull). - Output: An array or list of node values.
- Constraints:
- The tree can have up to approximately 10^5 nodes.
- Node values fit within a 32-bit integer.
Task 2: Sum of Top-Left Submatrix up to (x, y)
Given an m x n matrix A of integers and a coordinate (x, y) (0-indexed), compute the sum of all elements within the rectangle from (0,0) to (x,y) inclusive.
- Definition:
sum(x,y) = Σ_{i=0..x} Σ_{j=0..y} A[i][j] - Input:
- An
m x nmatrixAof integers. - One or more queries, each specifying coordinates
(x, y), where0 ≤ x < mand0 ≤ y < n.
- An
- Output: For each query, return the computed sum.
- Follow-up: If there are many queries, design a solution for fast per-query time.
- Constraints:
- Dimensions
mandncan be up to 10^3 or higher. - Matrix elements can be negative.
- Intermediate and final sums may require a 64-bit integer type to prevent overflow.
- Dimensions
AI interview prep
Core dilemma
For Task 1, the dilemma is choosing between recursive simplicity and iterative robustness for deep trees. For Task 2, it's balancing initial precomputation cost against achieving optimal O(1) query time for multiple range sum queries.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Traverse a tree and answer 2D prefix sums
Problem Description
You are presented with two independent coding tasks.
Task 1: In-order Traversal of a Binary Tree
Given the root of a binary tree, return an array or list containing the node values in in-order traversal.
- In-order traversal definition: Process the left subtree, then visit the current node, then process the right subtree.
- Input:
rootof the binary tree (may benull). - Output: An array or list of node values.
- Constraints:
- The tree can have up to approximately 10^5 nodes.
- Node values fit within a 32-bit integer.
Task 2: Sum of Top-Left Submatrix up to (x, y)
Given an m x n matrix A of integers and a coordinate (x, y) (0-indexed), compute the sum of all elements within the rectangle from (0,0) to (x,y) inclusive.
- Definition:
sum(x,y) = Σ_{i=0..x} Σ_{j=0..y} A[i][j] - Input:
- An
m x nmatrixAof integers. - One or more queries, each specifying coordinates
(x, y), where0 ≤ x < mand0 ≤ y < n.
- An
- Output: For each query, return the computed sum.
- Follow-up: If there are many queries, design a solution for fast per-query time.
- Constraints:
- Dimensions
mandncan be up to 10^3 or higher. - Matrix elements can be negative.
- Intermediate and final sums may require a 64-bit integer type to prevent overflow.
- Dimensions