Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Decision Tree From Scratch
00:00
5 left

Decision Tree From Scratch

HardPython

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):
Interviewer

Your question is Decision Tree From Scratch. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.