Cohort analysis in Digital Marketing - Time & Space Complexity
When performing cohort analysis, we want to understand how the time to analyze data changes as the number of users or time periods grows.
We ask: How does the work increase when we add more cohorts or more data points?
Analyze the time complexity of the following cohort analysis process.
// Assume cohorts is a list of user groups by signup date
// dataPoints is the number of time periods to analyze
for (let cohort of cohorts) {
for (let period = 0; period < dataPoints; period++) {
calculateMetric(cohort, period);
}
}
This code calculates a metric for each cohort over multiple time periods.
We have two loops repeating work:
- Primary operation: Calculating metrics for each cohort and time period.
- How many times: For each cohort, the inner loop runs once per time period, so total runs equal cohorts x dataPoints.
As we add more cohorts or more time periods, the total calculations grow by multiplying these two numbers.
| Input Size (cohorts x periods) | Approx. Operations |
|---|---|
| 10 cohorts x 5 periods | 50 calculations |
| 100 cohorts x 10 periods | 1,000 calculations |
| 1,000 cohorts x 20 periods | 20,000 calculations |
Pattern observation: Doubling cohorts or periods roughly doubles the work, so total work grows proportionally to both.
Time Complexity: O(n × m)
This means the time to complete cohort analysis grows proportionally with the number of cohorts and the number of time periods analyzed.
[X] Wrong: "The time only depends on the number of cohorts, not the time periods."
[OK] Correct: Each cohort's data is analyzed across all time periods, so ignoring periods misses a big part of the work.
Understanding how cohort analysis scales helps you explain data processing challenges clearly and shows you can think about efficiency in real marketing tasks.
"What if we only analyze a fixed number of recent time periods regardless of total data? How would that affect the time complexity?"