Quant research teams that fit gradient-boosted trees on market features spend most of their training time inside one routine, the per-feature split search, which open-source learners such as Microsoft’s LightGBM rebuild around histograms for speed. This screen (reported by candidates from several quant and applied-AI loops) asks you to write the exact version of that routine by hand. You implement best_split for one continuous feature in NumPy, and no tree library may be called inside the function.
The column x has shape and the labels
y are integers in . Your function returns the threshold
(left child:
) and its expected information gain under the Gini criterion
, or
(None, 0.0) when no split exists. The interviewer grades three things. Candidates must be midpoints of sorted unique values rather than observed values. The scan must stay at with no Python loop over classes. Degenerate columns must be handled honestly.
Complete the 4 missing operations in the skeleton below and trace the array shape after each line. Then verify your routine against a hand-computed two-threshold fixture and cross-check the threshold and gain with a depth-1 sklearn.tree.DecisionTreeClassifier on a toy dataset.
Skeleton Code (Fill Blanks 1-4):
01 import numpy as np
02
03
04 def gini(counts: np.ndarray) -> np.ndarray:
05 """Gini impurity from class counts of shape (..., C)."""
06 # TODO (Blank 2): 1 - sum_c p_c^2 over the class axis only,
07 # vectorized over any leading axes (no class loop)
08 impurity = ...
09 return impurity
10
11
12 def best_split(x, y, n_classes, min_samples_split=2):
13 """Best Gini threshold for one feature (left child: x <= threshold).
14 Returns (threshold, gain), or (None, 0.0) when no split exists."""
15 m = len(x)
16 # TODO (Blank 1): candidate thresholds, shape (K,), ascending
17 candidates = ...
18 # TODO (Blank 4): guard contract, return (None, 0.0) when this node
19 # or this column cannot be split
20 ...
21 order = np.argsort(x, kind="stable")
22 x_sorted, y_sorted = x[order], y[order] # (m,)
23 parent_counts = np.bincount(y_sorted, minlength=n_classes) # (C,)
24 parent_gini = gini(parent_counts)
25 # TODO (Blank 3): weighted Gini gain of all K candidates in one
26 # vectorized pass (no Python loop), shape (K,);
27 # best = argmax index, ties go to the lowest threshold
28 gains = ...
29 best = ...
30 return float(candidates[best]), float(gains[best])
Figure 1: the input and output contract is fixed and the sort is provided, while the question-marked steps between them are yours to write in plain NumPy.
1Inspection Checklist (Where to Look)
(1) Candidate Set: A useful threshold must change the partition. Count how many distinct partitions x <= t can produce, and make sure no candidate leaves a child empty.
(2) Impurity on Counts: The helper receives class counts, both as one row and as a
table. Reduce over the class axis only.
(3) Scan Budget: Count how many full passes over the rows your scan makes. The budget is one sort plus a constant number of linear passes, with no Python loop.
(4) Degenerate Inputs: List the inputs for which no honest threshold exists, and return (None, 0.0) for them before anything divides or indexes.
2Visual Review Map (Figure 2)

Figure 2: The shape trace a strong candidate narrates aloud. Every blank shows the target shapes it must produce; writing the operations that reach them is the exercise.
3Interviewer Nudge & Line Focus
Pin the input shape, output shape, and failure inputs of each blank before writing the NumPy call:
x = [2, 2, 5, 9], which thresholds produce genuinely different partitions under x <= t, and how many are there? If you used t = 9, what size would the right child have when you later divide by it?
(C,) for the parent and once per side with (K, C) for every candidate. Which axis must each row be normalized over, and what does a reduction without an axis argument return for the table case?









