FastPrepDecision Tree Classifier from Scratch
Problems/Decision Tree Classifier from Scratch

Decision Tree Classifier from Scratch

Zillow logoZillow● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Asked at Zillow in 2019, reported by a candidate for a Machine Learning Engineer Intern role in Seattle, Washington, who did not get an offer. The candidate had three in-person interviews of one hour each, held one after another, and calls it their first on-site. The report lists its questions round by round; the second round's question is a decision tree, worked on a whiteboard. Since the report ties it to round 2 of those in-person interviews, we list it as an onsite interview.

The report does not say whether the candidate was expected to write code or to walk through the method out loud; writing the code that grows the tree and makes predictions is our reading. The details of the task are our choices: the input form (rows of integer features with integer class labels, a depth limit and query rows), Gini impurity as the split measure, splits of the form feature <= value and their tie order, the stop rules, the most-common-label rule at leaves, exact comparison of scores, and the limits (up to 300 training rows and 300 queries, up to 6 features, values 0 to 100, labels 0 to 4, depth 0 to 8). The editorial also covers entropy, another common split measure.

Our version is about 50% the same as the reported question.

You are given n training rows. Row i has d integer features features[i][0], ..., features[i][d - 1] and a class label labels[i]. Grow one binary decision tree on these rows with the rules below, then return the label the tree predicts for each row of queries, in the same order.

Gini impurity. For a group of m rows in which ck rows have label k, the Gini impurity is G = 1 - (c0/m)2 - (c1/m)2 - ... - (c4/m)2. It is 0 when every row has the same label, and larger when the labels are more mixed.

Growing the tree. The root holds all n rows and has depth 0. A node with rows S and depth depth is handled like this:

  1. If depth == maxDepth, or all rows in S have the same label, the node is a leaf.
  2. Otherwise, every pair (f, v) is a candidate split, where f is a feature index and v is a value that feature f has in some row of S, except the largest such value. Rows with features[r][f] <= v go to the left child L and the other rows go to the right child R, so both children get at least one row.
  3. The score of a split is the weighted Gini impurity of its children: (|L| * G(L) + |R| * G(R)) / |S|, where |X| is the number of rows in X. Take the split with the lowest score. If several splits share the lowest score, take the smallest f, then the smallest v.
  4. If there is no candidate split (each feature has a single value in S), or the lowest score is not strictly less than G(S), the node is a leaf. Otherwise the node splits on that (f, v), and its two children have depth depth + 1 and are handled the same way.

A leaf predicts the label that appears most often among its rows. If several labels tie, it predicts the smallest of them.

Predicting. A query row starts at the root. At a node that splits on (f, v) it moves to the left child if query[f] <= v and to the right child otherwise. Its prediction is the label of the leaf it reaches.

Exact scores. Compare impurities and scores exactly. Each one is a fraction of whole numbers, so two of them can be compared by cross-multiplying 64-bit integers. Floating-point values may round two equal scores differently and pick a different split.

Function

decisionTreePredict(features: int[][], labels: int[], maxDepth: int, queries: int[][]) → int[]

Examples

Example 1

features = [[1,1],[1,2],[1,3],[2,2],[3,2],[3,3]]labels = [0,0,1,1,1,1]maxDepth = 2queries = [[1,2],[3,1],[1,5]]return = [0,1,1]

The root holds all 6 rows: two with label 0 and four with label 1, so G = 1 - (2/6)2 - (4/6)2 = 4/9. Its candidate splits are feature 0 at 1 or 2 and feature 1 at 1 or 2 (3 is the largest value of both features). Their scores are 2/9, 1/3, 4/15 and 1/3.

The lowest is 2/9, below 4/9, so the root splits on feature 0 at 1: rows 0, 1, 2 go left and rows 3, 4, 5 go right. The right child's rows all have label 1, so it is a leaf that predicts 1.

The left child (depth 1) has labels 0, 0, 1. Feature 0 is 1 in all three rows, so it can split only on feature 1: at 1 the score is 1/3, at 2 it is 0. It splits at 2 into a leaf with rows 0 and 1 (predicts 0) and a leaf with row 2 (predicts 1).

Query [1,2] goes left (1 <= 1), then left (2 <= 2): 0. Query [3,1] goes right: 1. Query [1,5] goes left, then right (5 > 2): 1.

Example 2

features = [[0,0],[0,1],[1,0],[1,1]]labels = [0,1,1,0]maxDepth = 2queries = [[0,1],[1,1]]return = [0,0]

The root has two rows of each label, so G = 1 - (1/2)2 - (1/2)2 = 1/2. Each feature can split only at 0, and both splits leave one row of each label on each side, so both scores are 1/2. That is not strictly less than 1/2, so the root is a leaf.

Labels 0 and 1 each appear twice, so the leaf predicts the smaller one, 0, for every query.

Example 3

features = [[1,1],[2,2],[3,3]]labels = [0,0,1]maxDepth = 1queries = [[3,1],[1,3]]return = [1,0]

The splits on feature 0 at 2 and on feature 1 at 2 both send rows 0 and 1 left and row 2 right, so both score 0; the splits at 1 score 1/3. The tie goes to the smaller feature, so the root splits on feature 0 at 2.

Query [3,1] goes right (3 > 2) and gets 1. Splitting on feature 1 instead would have sent it left, to label 0. Query [1,3] goes left and gets 0.

Constraints

  • 1 <= n == features.length == labels.length <= 300
  • 1 <= d == features[i].length == queries[j].length <= 6
  • 0 <= features[i][k], queries[j][k] <= 100
  • 0 <= labels[i] <= 4
  • 0 <= maxDepth <= 8
  • 1 <= queries.length <= 300

More Zillow Onsite Interview questions

  • Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
  • Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
  • First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
  • Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
  • Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
  • Tic-Tac-Toe Winner on a Huge BoardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2016
  • Most Frequent Three-Page SequencesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2016
  • Maximum Path Sum in a GridONSITE INTERVIEW · Seen Apr 2016

Similar questions at other companies

  • Preorder of a Special Binary TreeNielsen · OA
  • Decision Tree Regression from ScratchNVIDIA · OANVIDIA interview questions
  • Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions
  • Random Forest Classifier from ScratchMicrosoft · PHONE SCREENMicrosoft interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
  • Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
  • Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
  • Merge Sort Report with a Noisy ComparatorMercor · ONSITE INTERVIEWMercor interview questions
  • Top-Down Employee Reporting ForestOkta · ONSITE INTERVIEWOkta interview questions
All Zillow interview questionsSee Zillow hiring insights
Saved
class Solution {
    public int[] decisionTreePredict(int[][] features, int[] labels, int maxDepth, int[][] queries) {
        // Write your code here.
    }
}
Checking account…