Reported September 2026
TikToktree

Decision Tree Classifier from Scratch

Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live TikTok OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt at this TikTok problem is treating it like a machine learning task instead of a tie-breaking spec. It was reported in September 2026, and the OA asks you to build a decision tree classifier from scratch: entropy, information gain, best split, then prediction by tree traversal. No libraries. The tree itself is easy. The grader's exact rules for thresholds, ties and leaves are where people lose test cases. If you blank on the details mid-assessment, StealthCoder runs invisibly as a safety net, but the details below are what you need to know first.

The problem

Your task is to implement parts of the Decision Tree classification algorithm from scratch (i.e., without importing any libraries or packages). As a reminder, the Decision Tree building comprises four major steps:
Build tree nodes, where inner nodes contain feature index and value based on which the decision should be made, and leaf nodes contain a class label to predict.
Find the best split of samples for each tree node.
Calculate entropy to measure the purity of a split using the following formula.E = -Σᵢ₌₁ᴺ pᵢ · log₂ pᵢ, where pᵢ = Nᵢ/N
where Nᵢ is the number of occurrences of a specific class label in the labeled data, and N is the number of samples.
Calculate the Information Gain of a split using the following formula.IG = E_parent - (l/n · E_left + r/n · E_right)
where l is the number of samples in the left child, r is the number of samples in the right child, and n = l + r is the total number of samples.
To predict class labels of unlabeled samples, one needs to traverse the tree according to the information in the nodes and assign the leaf node label.
To validate the algorithm implementation, you will need to use it for some classification tasks. Specifically, you will be given a two-dimensional array of float values x_train as training data, where each sub-array x_train[i] represents a unique case, with one-dimensional array y_train where each element represents the true class label of corresponding sub-array in x_train[i].
FastPrep execution adapter
The source image is cropped before the rest of the callable contract and does not show deterministic split or tie rules. FastPrep considers each non-maximum observed feature value as a threshold, sends values less than or equal to it left, chooses equal-gain splits by smaller feature index and then smaller threshold, stops on non-positive gain, and resolves a tied leaf majority with the smaller label. These are grading-adapter rules, not source-image wording.

Function
decisionTreePredictions(xTrain: double[][], yTrain: int[], xTest: double[][]) → int[]

Examples
Example 1
xTrain = [[0], [1], [2], [3]]
yTrain = [0, 0, 1, 1]
xTest = [[0.5], [2.5]]
return = [0, 1]
FastPrep-authored runnable example (not shown in the source image): Splitting feature 0 at threshold 1 creates two pure children. The first test sample goes left and the second goes right.

Constraints
FastPrep execution-adapter constraints (not shown in the source image):
1 <= xTrain.length == yTrain.length <= 120.
Each sample has between 1 and 8 features.
Every training and test row has the same number of features.
All feature values are finite and every class label is an integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is recursive tree building. At each node, try every feature and every observed value except the maximum as a threshold. Send values less than or equal to the threshold left. Compute entropy for parent and children, then information gain as parent entropy minus the weighted child entropies. Pick the highest gain. On equal gain, take the smaller feature index, then the smaller threshold. Stop when the best gain is not positive, or when the node is pure, and make a leaf. A tied leaf majority returns the smaller label. The classic pitfall is using a strict greater-than comparison in the wrong place, or breaking ties by iteration order that doesn't match the rules. Another is computing log2 of zero when a class count is 0, so skip those terms. Keep float comparisons consistent. Prediction just walks down using the same less-than-or-equal test. StealthCoder is the hedge if you freeze on the recursion during the live OA.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Decision Tree Classifier from Scratch cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass TikTok's OA.

TikTok reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Decision Tree Classifier from Scratch FAQ

What's the trick in the TikTok decision tree OA?+

Follow the tie rules exactly. Candidate thresholds are observed values except the maximum, left means less than or equal, and equal gains go to the smaller feature index then the smaller threshold. Most failures come from tie handling, not from the entropy math.

How do I compute entropy without crashing?+

Count labels, divide each count by the sample total, and sum -p * log2(p). Only iterate over classes that actually appear, so p is never zero. A pure node gives entropy 0. That avoids log of zero without any special casing.

When should the tree stop splitting?+

Stop when the best information gain is not positive, or when all labels in the node match, since no split can help. Then make a leaf with the majority label. If labels tie in count, return the smaller label.

Is this hard given the constraints?+

Not on performance. Up to 120 samples and 8 features means brute force over every feature and threshold is fine. The difficulty is writing clean recursion and matching the grader's rules. Medium difficulty, mostly careful implementation.

How do I prepare in 48 hours?+

Write the tree from scratch once. Do entropy, gain, best split, build and predict as separate functions. Test on the sample where xTrain is [[0],[1],[2],[3]] and expect [0, 1]. Then test a tie case and a single-class case. That covers most hidden-test traps.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with TikTok.

OA at TikTok?
Invisible during screen share
Get it