# 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.

AI and LLM interview question Q431, Classic ML and deep-learning basics. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/decision-trees-and-random-forests

## Say it in 30 seconds

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.

## Say it in 2 minutes

A decision tree is a chain of yes-or-no questions about a case, a bit like a triage nurse's checklist. Each answer picks the next question, and the last box, the leaf, gives the prediction. To build one, the computer tries many possible questions and keeps the split that sorts past cases into the purest groups. It repeats that inside each group until it hits a limit.

The weakness is variance, meaning how much its answers move when the data changes. A deep tree can memorise the noise in its training data, so a few unusual cases can change its questions, and its answers swing when the data changes. That is overfitting: it looks great on old files and weaker on new ones.

A random forest fixes this by building many trees. Each tree sees a random copy of the rows, drawn with repeats, and only a random subset of the features at each split. The trees differ from one another, and their answers are combined, so the mistakes of one tree are often cancelled by the others.

The trade-off is explanation. One small tree can be read as a few rules, which a bank needs when it declines a loan. A forest is usually steadier on new cases, but it has no short list of rules. So I would start with a small tree if every decision must be explained, and test the forest against it.

## 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.

- **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.

*Diagram, One loan file walks down a learned tree: 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. The applicant's file reaches the first question.
2. At 45% or less, the group is mostly safe: 13 in 100 defaulted.
3. Above 45%, the group is riskier: 37 in 100 defaulted, so the tree splits it again.

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. **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. **Keep the best split:** Pick the split that makes the groups purest, then repeat the same search inside each new group.
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.

*Diagram, One deep tree or a forest of trees: 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.*
1. Tuned to last year's files, the deep tree misses patterns in new ones.
2. Averaging many trees evens out the mistakes of each one.

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.

*Diagram, How a random forest builds its answer: 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.*
1. Each tree starts from its own random copy of the files, with repeats allowed.
2. The second copy holds a different mix of the same files.
3. Each copy trains one tree, which picks each split from a random set of features.
4. Copy 2 trains Tree 2 with a different random set of features at each split.
5. Tree 1 gives its answer.
6. Tree 2 gives its answer.
7. The answers are combined into one prediction.

> "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." Better: A 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." Better: It is many trees, each trained on a different sample and set of features, then combined.
- Trap: "Random forests never overfit." Better: Forests 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?

A. The tree has no depth limit and scored 99% on last year's claims, but 61% on this month's claims.
B. We retrain the model every quarter as new claims arrive.
C. The forest uses 200 trees, each trained on a random sample of past claims.
D. We keep a test set of claims from a later month, never used in training.

**Answer:** A. A big drop from old to new claims is the sign of overfitting: the tree memorised last year's claims. A depth limit or a forest reduces it, and a clean test set from a later month is how you catch it.

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

A. A random forest of 500 deep trees
B. One deep tree grown with no limit
C. One small decision tree with a short list of rules
D. A neural network with three hidden layers

**Answer:** C. A small tree can be read as a short list of rules, which is what the bank needs to explain a decline. The forest and the deep tree are harder to read, and a neural network is harder still.

## Can you answer these from memory?

**Flash cards**

- **What is a decision tree?** A flowchart of yes-or-no questions that ends in a prediction at each leaf.
- **How does a tree pick a split?** It tries many questions and keeps the one that makes the groups purest.
- **Why does one tree overfit?** It memorises noise in its training data, so its answers swing with new data.
- **What is a bootstrap sample?** A random copy of the training rows, drawn with repeats allowed.
- **How does a forest fix overfitting?** It averages many trees, each trained on random rows and random features.
- **What is the trade-off?** Forests are usually steadier, but harder to explain than one small tree.

## Likely follow-up questions

- Q439 Bagging vs boosting? https://designervenkat.online/interview/ai/bagging-vs-boosting
- Q412 Bias vs variance? https://designervenkat.online/interview/ai/bias-vs-variance
- Q413 Overfitting vs underfitting? https://designervenkat.online/interview/ai/overfitting-vs-underfitting
- Q436 How do you tune hyperparameters? https://designervenkat.online/interview/ai/how-do-you-tune-hyperparameters
- Q429 What is early stopping? https://designervenkat.online/interview/ai/what-is-early-stopping

## Learn it in depth

- [Decision trees and random forests](https://designervenkat.online/learn/machine-learning/decision-trees-random-forest)
