FastPrepDecision Tree Regression from Scratch

Decision Tree Regression from Scratch

NVIDIA logoNVIDIA● HardNEW GRADOA
Learn

Problem statement

Build a decision-tree regression model from scratch using the training rows trainX and their numeric targets trainY. Return one prediction for each row of queryX, in query order. Implement split selection, recursive construction and prediction traversal directly without a machine-learning or tree-fitting library.

For this exercise, assume all features and training targets are integers. Every training and query row has the same positive number of features. Repeated training rows remain separate sample occurrences.

At each node, consider every feature. Sort its distinct values among that node's samples and consider the midpoint between every two consecutive values as a split threshold. A sample goes left when its feature value is less than or equal to the threshold; otherwise, it goes right. Each candidate has two nonempty groups.

For a group G, its prediction is the arithmetic mean mean(G) of its targets, and its squared error is SSE(G) = sum((y - mean(G))^2). Choose the split minimizing SSE(left) + SSE(right). Use exact mathematical values for split comparisons. On equal errors, prefer the smaller feature index; if that also ties, prefer the smaller threshold.

The root has depth 0. A node becomes a leaf if its depth equals maxDepth, no valid split exists, or the best split does not strictly reduce the node's squared error. Otherwise, build both children using the same rules. Every leaf predicts the mean of all sample occurrences that reach it, including when their targets differ.

Traverse the fitted tree for each query using the same inclusive-left split rule and return the reached leaf's mean. Each prediction is judged with absolute tolerance 10^-6.

What the interview report shared

The report asks for a decision-tree regression model built from scratch. It does not disclose a dataset, split criterion, depth or stopping rule, tie convention, or training and prediction interface. The deterministic rules above define this practice version.

Function

decisionTreeRegression(trainX: int[][], trainY: int[], queryX: int[][], maxDepth: int) → double[]

Examples

Example 1

trainX = [[0],[2],[4],[6]]trainY = [0,0,10,10]queryX = [[1],[3],[5]]maxDepth = 1return = [0.0,0.0,10.0]

The best root threshold is 3: the two groups have means 0 and 10, with zero total squared error. Query 3 follows the inclusive-left rule and predicts 0.

Example 2

trainX = [[0,0],[0,2],[2,0],[2,2]]trainY = [0,0,10,10]queryX = [[1,1],[2,0],[0,2]]maxDepth = 2return = [0.0,10.0,0.0]

Splitting feature 0 at 1 gives pure target groups with means 0 and 10. Splitting feature 1 does not reduce squared error. The first query lies exactly on the chosen threshold and goes left.

Example 3

trainX = [[-3],[5],[9]]trainY = [-2,4,7]queryX = [[-1000],[0],[1000]]maxDepth = 0return = [3.0,3.0,3.0]

The root is already at the depth limit. It remains a leaf and predicts the target mean (-2 + 4 + 7) / 3 = 3 for every query.

Constraints

  • For this exercise, assume 1 <= trainX.length == trainY.length <= 60.
  • Every training and query row has the same number of features d, with 1 <= d <= 5.
  • 1 <= queryX.length <= 60.
  • Every feature value and training target is an integer in [-1000,1000].
  • 0 <= maxDepth <= 6.

More NVIDIA problems

See NVIDIA hiring insights
public double[] decisionTreeRegression(int[][] trainX, int[] trainY, int[][] queryX, int maxDepth) {
    // Fit the tree and return predictions in query order.
}
trainX[[0],[2],[4],[6]]
trainY[0,0,10,10]
queryX[[1],[3],[5]]
maxDepth1
expected[0.0,0.0,10.0]
Checking account…