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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Decision Tree in Python
00:00
5 left

Decision Tree in Python

HardPython

Problem

Airbus Americas Customer Services can use a compact decision tree to classify service records from numeric signals such as aircraft age or delay duration. Implement a deterministic CART-style classifier that chooses binary splits using Gini impurity.

Given feature matrix X, class labels y, a maximum tree depth, and a minimum split size, return the trained tree as nested dictionaries.

Formal Specification

  • X is a non-empty list of n rows, each containing d numeric features.
  • y is a list of n labels. Labels may be integers or strings.
  • max_depth is a non-negative integer. The root has depth 0.
  • min_samples_split is a positive integer.
  • An internal node must have the form {"type": "node", "feature": f, "threshold": t, "left": left_tree, "right": right_tree}.
  • A leaf must have the form {"type": "leaf", "class": label}.
  • Use x <= threshold for the left branch and x > threshold for the right branch.

At each node, consider every feature and every midpoint between adjacent distinct sorted feature values. Select the split with the lowest weighted Gini impurity. Break equal-score ties by feature index, then by threshold order. Stop when the node is pure, reaches max_depth, has fewer than min_samples_split records, or has no valid split. A leaf predicts the majority class, breaking class ties by first appearance in the node.

Constraints

  • 1 <= n <= 200
  • 1 <= d <= 20
  • 0 <= max_depth <= 20
  • 1 <= min_samples_split <= n
  • Feature values are finite numbers
  • Every row has the same number of features

Function Signature

def decision_tree(X, y, max_depth, min_samples_split):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
CodePython 3
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.