Algorithms
Understanding time complexity and Big O notation from the ground up
Learn how to quantify the execution efficiency of your code across varying input sizes using Big O notation, asymptotic analysis, and real-world Python examples.
Plain words. One step at a time. Nothing here assumes you already know the idea.
The idea
Time complexity is a mathematical characterization of how the runtime of an algorithm increases as the size of its input grows. Big O notation expresses this relationship by defining an upper bound on growth, abstracting away hardware differences and low-level constant factors.
Imagine delivering parcels around a city.
You will need. Basic familiarity with Python syntax · Understanding variables, loops, and conditional statements
One pass
Step 1 of 3
When evaluating how fast an algorithm runs, measuring wall-clock time in seconds is unreliable. Running the same code on a slow phone produces a different duration than running it on a supercomputer. Background tasks, operating system scheduling, and hardware architectures also distort measurements. Instead of tracking elapsed seconds, computer scientists count the number of fundamental computational steps executed by the code relative to the size of the input.
Smallest version
Comparing constant and linear time algorithms on an input list
def get_first_element(numbers):
# Constant time: O(1)
# Always executes exactly one index lookup, regardless of list size.
if not numbers:
return None
return numbers[0]
def calculate_sum(numbers):
# Linear time: O(N)
# The loop runs once for every element in the list.
total = 0
for value in numbers:
total += value
return total
# Demonstration
sample = [10, 20, 30]
print(get_first_element(sample)) # 1 step
print(calculate_sum(sample)) # 3 stepsKeep these
- Time complexity measures how operation counts scale with input size, not wall-clock seconds.
- O(1) represents constant time, meaning runtime does not depend on input size.
- O(N) represents linear time, where running time grows in direct proportion to input size.
Try one
Analyze an accumulator loop with early exit
easy
Also on MonksBit
- Algorithm representation, from flowcharts and pseudocode to executable code Learn how to express computational logic clearly using flowcharts, structured pseudocode, and step-by-step trace tables before translating them into robust Python implementations. Algorithms · 7 min
- Introduction to algorithms, characteristics, and practical design Understand the precise definition of an algorithm, identify its fundamental characteristics, and analyze time and space complexity with concrete Python implementations. Algorithms · 6 min
- How to Choose the Right Data Structure for Your Algorithm Learn how to match programmatic access patterns, update frequencies, and memory budgets to the most efficient data structure. Data Structures · 8 min
- Fundamental data structure operations: insertion, deletion, searching, and traversal Master the mechanics, algorithmic complexity, and practical trade-offs of the four primary data structure operations across contiguous and linked memory layouts. Data Structures · 10 min
- Time and space complexity, from counting operations to asymptotic bounds Analyze algorithms to determine their computational runtimes and memory requirements across different scales of input. Data Structures · 7 min
- Data structures and algorithms, from core concepts to system design Learn how data structures organise memory, how algorithms operate on that memory, and how to select complementary pairs to design scalable systems. Data Structures · 8 min
- Static vs. Dynamic Data Structures: Fixed vs. Growable Memory Learn the mechanical differences between static and dynamic data structures, implement fixed-capacity and resizable arrays, and choose the right memory model. Data Structures · 8 min
- Types of Data Structures: Linear vs. Non-Linear Explained Learn how linear and non-linear data structures organise memory, navigate sequential versus branching relationships, and guide algorithm design. Data Structures · 7 min
- Database Indexing & Query Optimization: A Complete Guide from Scratch Understand how relational database indexes store data, accelerate lookups, and guide query planning across real-world systems. System Design · 9 min
- Database Schema Design & Data Modeling: From Conceptual Entities to Physical Storage Design clean, normalized database schemas, handle relationships, enforce relational integrity, and optimize storage with practical indexing and denormalization strategies. System Design · 9 min
- SQL vs. NoSQL Databases: From Data Modeling to Distributed Scaling Learn the mechanical differences between relational and non-relational databases, how data models impact scaling, and how to select the right persistence layer. System Design · 10 min