Linear regression typically has a time complexity of O(n) for training with stochastic gradient descent, while decision trees have an average time complexity of O(n log n) for training. Understanding these complexities helps in selecting the appropriate algorithm based on dataset size and required performance.
In depth
The time complexity of algorithms is crucial in machine learning, as it directly influences the efficiency and scalability of model training. For linear regression using stochastic gradient descent, each update of the weights takes constant time, and iterating through the dataset n times results in a complexity of O(n) per iteration. However, the algorithm can take multiple iterations to converge, thus making the overall complexity potentially O(n * k), where k is the number of iterations. In contrast, decision trees involve sorting and partitioning the dataset, leading to an average time complexity of O(n log n) for building the tree. This difference becomes significant when working with large datasets, where linear regression may provide quicker training times, but less complex models like decision trees may be more computationally expensive yet offer greater interpretability and performance in non-linear scenarios. Adjusting parameters like max depth in decision trees can also impact complexity and training time significantly.
Example scenario
In a project to predict housing prices, we used both linear regression and decision trees to compare their performance. With a dataset of 100,000 samples, the linear regression model trained quite fast, completing in a few seconds due to its O(n) complexity. However, the decision tree model took considerably longer since it had to sort and evaluate splits, resulting in training times of several minutes. Ultimately, while the decision tree provided better accuracy due to its ability to model complex relationships, it required careful consideration of training time during deployment.
Common mistakes
One common mistake is assuming that all machine learning algorithms will perform similarly regardless of dataset size. A candidate might overlook how algorithmic complexity affects performance when scaling to larger datasets, potentially leading to inefficient choices. Another mistake is not considering the interplay of time complexity with hyperparameters; for example, changing the depth of a decision tree can dramatically influence training time and model performance, but candidates may underestimate this relationship during algorithm selection.
Follow-up questions
- How would you estimate the training time for a new dataset?
- Can you explain how feature selection affects time complexity?
- What strategies can you use to optimize the training time of a decision tree?
- How would you compare the performance of linear regression versus polynomial regression in terms of complexity?

Preparing for a system design interview?
- A 7-step method for answering any system design question, plus estimation numbers
- 5 worked designs: URL shortener, rate limiter, chat, news feed and file storage
- Guides to caching, databases, queues, consistency and reliability, with a 4-week study plan
- 306 practice questions with answers on caching, databases, queues, microservices, APIs and more
5 worked designs, a 4-week plan and 306 practice questions with answers.
The material stays free to read online. This is the organised, offline, print-ready PDF edition. Secure payment by PayPal; your personal download link is emailed after payment is confirmed. Digital product, no shipping.