Decision Tree Regression from Scratch
Updated
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.
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. A prediction is correct if it is within 10^-6 of the expected value.
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
1 <= trainX.length == trainY.length <= 60.- Every training and query row has the same number of features
d, with1 <= d <= 5. 1 <= queryX.length <= 60.- Every feature value and training target is an integer in
[-1000,1000]. 0 <= maxDepth <= 6.
More NVIDIA Online Assessment questions
- Query a Header-Encoded MatrixOA · Seen Oct 2026
- Maximum ScoreOA · Seen Oct 2026
- Address on Same PageOA · Seen Oct 2026
- Minimize Task Resource ConsumptionOA · Seen Oct 2026
- Minimum Project Bid CostOA · Seen Oct 2026
- Count Ideal Numbers in a RangeOA · Seen Oct 2026
- Generate Bit MaskOA · Seen Oct 2026
- Reconstruct an Array from Prefix XOROA · Seen Oct 2026
- Sum of Subarray MinimumsOA · Seen Oct 2026
- Unique Integers That Sum to ZeroOA · Seen Oct 2026
Similar questions at other companies
- Preorder of a Special Binary TreeNielsen · OA
- Decision Tree Classifier from ScratchZillow · ONSITE INTERVIEWZillow interview questions
- Temporal AnomaliesInMobi · OAInMobi interview questions
- Possible Timer Intervals with One Wrong ReadingIndeed · OAIndeed interview questions
- Allocate Mailboxes with Squared DistanceLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Random Forest Classifier from ScratchMicrosoft · PHONE SCREENMicrosoft interview questions
- Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Best Restart Cutoff for a Long-Tailed JobOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Delete and Freeze GameRubrik · OARubrik interview questions
- Maximum Signal Walk in a Server TreeTower Research Capital · OA