esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

Decision Trees and Random Forests

How a decision tree asks yes-or-no questions, why one tree overfits, and how a random forest averages many trees to fix it, for your AI interview.

In short

A decision tree sorts a case by asking a chain of yes-or-no questions about its features. A random forest trains many trees on random samples and random features, then combines their answers, so the result is steadier than a single deep tree.

12 min readBeginnerCommon · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

A decision tree sorts a case by asking a chain of yes-or-no questions, and each answer picks the next one. One deep tree can memorise its training data and swing when new data arrives. A random forest fixes that by training many trees on random rows and features, then combining their answers. The result is steadier, but harder to explain than one small tree.

What is a decision tree and a random forest, in plain words?

A decision tree is a flowchart that a computer learns from past cases. It asks one question at a time about a new case, and each answer sends the case down one branch. The box at the end of a branch, called a leaf, gives the prediction.

Think of a triage nurse in an emergency room. She asks a short run of questions: is the patient breathing, is there chest pain, what is the blood pressure. Each answer decides the next question, and the last one sets the priority. A decision tree works the same way, except the questions are learned from data instead of written by hand.

A random forest is a crowd of many such trees. Each tree learns from a slightly different set of past cases and sees only some of the features. The forest then combines their answers. Many different, imperfect trees usually make fewer mistakes together than one tree alone.

Key words

Decision tree
A flowchart of yes-or-no questions that ends in a prediction.
Split
One question in a tree, such as whether debt is above 45% of income.
Leaf
The last box of a branch, which holds the prediction.
Bootstrap sample
A random copy of the training rows, drawn with repeats allowed.
Ensemble
Many models whose answers are combined into one.

How does a tree choose its questions?

A tree tries many possible questions and keeps the one that sorts the past cases best. Sorting well means each group ends up with mostly one answer, such as mostly defaulters or mostly safe borrowers. Scikit-learn's guide says practical tree learners are greedy: they make a locally optimal decision at each node.

One loan file walks down a learned tree

New loan applicant

debt is 52% of income (made-up)

Debt above 45%?

the first split the tree learned

Groups after the first split

Lower-risk group

13 in 100 defaulted

Higher-risk group

37 in 100 defaulted: split again

  1. 1The applicant's file reaches the first question.
  2. 2At 45% or less, the group is mostly safe: 13 in 100 defaulted.
  3. 3Above 45%, the group is riskier: 37 in 100 defaulted, so the tree splits it again.
A made-up loan file walks down a learned tree. One question splits 1,000 past applications into two groups with very different default rates. The riskier group gets split again, and the tree keeps going until a limit stops it.
  1. 1

    Try many questions

    Each feature, at many cut points, is a candidate question. For a loan file, that might be debt above 40% of income, above 45%, above 50%, and so on.

  2. 2

    Score each split

    Measure how mixed each resulting group is. Scikit-learn offers an entropy score, which is a standard way to measure that mix.

  3. 3

    Keep the best split

    Pick the split that makes the groups purest, then repeat the same search inside each new group.

  4. 4

    Stop at a limit

    Without a limit, a tree keeps splitting until its leaves are pure, meaning every case in a leaf has the same answer. Settings such as a maximum depth stop that.

Worked example, made-up numbers: a lender has 1,000 past loan applications, and 200 of them defaulted, which is one in five. In this pile, the best first question is whether debt is above 45% of income. The applicants at or below that line number 700, and 90 of them defaulted, about 13%. The 300 above it include 110 defaults, about 37%. The two groups are far more different than the whole pile, so the split did its job. The high-debt group then gets another split, perhaps on income.

Why does one tree overfit?

A deep tree keeps asking questions until they fit its training data closely. That makes it sharp on the cases it has seen and shaky on new ones. Trees are also unstable: a small change in the training data can produce a tree that looks quite different. The scikit-learn guides say it in these words:

  • “Decision trees can be unstable because small variations in the data might result in a completely different tree being generated.”

    scikit-learn, decision tree guide

  • “Indeed, individual decision trees typically exhibit high variance and tend to overfit.”

    scikit-learn, ensemble guide

Worked example, made-up numbers: a deep tree gets 98 of 100 old applications right, but only 76 of 100 new ones. It has learned last year's quirks, such as a postcode that happened to hold several defaults, and treats them as real rules. That gap between old and new data is overfitting.

One deep tree or a forest of trees

One deep tree

Fits last year's files

memorises the noise in the files

Random forest

Averages many trees

each tree sees random rows

Weak on new files

too close to the old data

Steadier on new files

errors partly cancel out

  1. 1Tuned to last year's files, the deep tree misses patterns in new ones.
  2. 2Averaging many trees evens out the mistakes of each one.
Two ways to learn from the same files. The single deep tree fits its own data too closely, so its answers swing with new files. The forest averages many trees, so its answers move less when the data changes.

The first fix for one tree is to limit its size. Scikit-learn's guide points to a maximum depth setting, max_depth, which controls how big the tree grows and so helps stop overfitting. A limit helps one tree, but it can also make each tree too weak, which is why forests take a different route.

How does a random forest work?

A random forest adds two kinds of randomness, so its trees differ from one another. Google's machine learning course describes a random forest as an ensemble of decision trees, each trained with its own random twist, such as a random set of rows. Random forests are the most popular form of decision tree ensemble, according to Google's course. Their trees are grown without pruning (cutting back branches that add little), so each one can overfit on its own.

  • Random rows

    Each tree is built from a sample drawn with replacement from the training data, a bootstrap sample, so every tree sees a different mix of rows.

  • Random features

    Attribute sampling means that at each node a tree tests only a random subset of the features, not all of them.

  • Combined answers

    Scikit-learn's forests average the trees' probabilities. The original method had each tree vote for one class.

How a random forest builds its answer

Past loan files

made-up: 1,000 applications

Random copies

Sample 1

rows drawn with repeats

Sample 2

a different mix of rows

Trees

Tree 1

random features at each split

Tree 2

random features at each split

Vote or average

the trees' answers are combined

Final prediction

steadier than one deep tree

  1. 1Each tree starts from its own random copy of the files, with repeats allowed.
  2. 2The second copy holds a different mix of the same files.
  3. 3Each copy trains one tree, which picks each split from a random set of features.
  4. 4Copy 2 trains Tree 2 with a different random set of features at each split.
  5. 5Tree 1 gives its answer.
  6. 6Tree 2 gives its answer.
  7. 7The answers are combined into one prediction.
A real forest has many trees, and two are shown here. Each tree sees its own random copy of the past files and a random set of features at each split. Their answers are combined, so no single odd tree decides.
  • “Random forests achieve a reduced variance by combining diverse trees, sometimes at the cost of a slight increase in bias.”

    scikit-learn, ensemble guide

Worked example, made-up numbers: a bank screens loans with 8 features, such as income, debt ratio, years with the bank and missed payments. One tree might always start with debt ratio, because it is the strongest signal. In the forest, each split tests only a few random features, so some trees start with income or missed payments instead. They make different mistakes, and averaging cancels many of them out.

Tip

Interview trade-off: limiting each tree's depth helps a single tree, but it can hurt the forest. Google's course says that aggressively reducing the variance of each tree improves each tree's quality but degrades the forest's accuracy. Scikit-learn's guide says the variance drop often wins anyway, which is why forests are usually better on new data.

How would you choose between one tree and a forest for a loan screen?

A bank must explain why an applicant was declined. Scikit-learn's guide lists being simple to understand and to interpret as an advantage of trees. A single small tree can be read as rules: a question, an answer, and the next question. A forest of many trees has no single list of rules, so explaining one decline needs extra tools.

  • One small tree

    Easy to explain

    You can read its questions as rules for each applicant. It is often less accurate on new cases than a forest, and it overfits when left deep.

  • Random forest

    Usually steadier

    Its answers move less with the training data, so it often predicts new applicants better. Explaining one decline needs extra tools.

My answer: start with a small tree if the bank must explain every decline, and measure the forest against it on new files. If the forest is clearly better and the bank has explanation tools, use the forest. Say the trade-off out loud; interviewers listen for it.

What does the interviewer want to hear?

A strong answer covers

  • A plain picture: a tree is a chain of yes-or-no questions that ends in a leaf.
  • How a split is chosen: many candidate questions, scored by how mixed the groups are.
  • Why one tree overfits: it memorises noise, so its answers swing with new data.
  • How a forest fixes it: random rows, random features and combined answers.
  • The trade-off: forests are usually steadier, but harder to explain than one small tree.
  • Limits: depth settings for one tree, and how many trees to use in the forest.

Common traps

  • Trap"A deeper tree is always more accurate."

    BetterA deeper tree fits the training data better, but often does worse on new data. Check it on a test set: cases the model never saw during training.

  • Trap"A random forest is just one big tree."

    BetterIt is many trees, each trained on a different sample and set of features, then combined.

  • Trap"Random forests never overfit."

    BetterForests overfit far less than one tree, but very deep trees and too little data can still cause trouble.

Can you spot the problem?

Spot the problem

A claims team reports these made-up results for its fraud tree. Which line is the red flag?

Quick check

A bank must explain every decline to regulators and customers. Which model fits that need best?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q439Bagging vs boosting?11 min
  2. Q412Bias vs variance?12 min
  3. Q413Overfitting vs underfitting?10 min
  4. Q436How do you tune hyperparameters?10 min
  5. Q429What is early stopping?10 min

Learn it in depth

Last updated on 27 November 2026

Building a model that has to explain its decisions?

I design and build AI products and the systems behind them. Book a short call and say what your model has to decide and who needs to understand why.