Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Decision Tree From Scratch

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Decision Tree From Scratch. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Calico Life Sciences needs a lightweight baseline classifier for a research modeling pipeline. Implement a binary decision tree classifier without using machine learning libraries.

Given numerical training features, binary labels, and test features, recursively choose the feature and threshold that produce the greatest weighted reduction in Gini impurity. Stop splitting when the node is pure, reaches max_depth, contains fewer than min_samples_split samples, or has no valid threshold. Each leaf predicts the majority label, with ties resolved in favor of 0.

Formal Specification

Implement decision_tree_predict(X_train, y_train, X_test, max_depth, min_samples_split). X_train is a non-empty list of rows, each row containing the same number of numeric features. y_train contains one binary integer label per training row. X_test contains rows with the same feature count. Return a list of predicted binary labels in the same order as X_test.

At every split, evaluate thresholds between adjacent distinct sorted feature values. Select the split with the lowest weighted Gini impurity. Break equal-score ties by choosing the lowest feature index, then the lowest threshold.

Constraints

  • 1 <= len(X_train) <= 2,000
  • 1 <= len(X_test) <= 500
  • 1 <= len(X_train[i]) <= 20
  • len(y_train) == len(X_train)
  • Each label is either 0 or 1
  • Feature values are finite numbers
  • 0 <= max_depth <= 20
  • 2 <= min_samples_split <= len(X_train)

Function Signature

def decision_tree_predict(X_train, y_train, X_test, max_depth, min_samples_split):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output