Customer lifetime value (CLV) calculation in Digital Marketing - Time & Space Complexity
When calculating Customer Lifetime Value (CLV), it's important to understand how the time to compute grows as we consider more customers or transactions.
We want to know how the calculation effort changes when the input data size increases.
Analyze the time complexity of the following CLV calculation code snippet.
// Assume customers is a list of customer data
let totalCLV = 0;
for (let customer of customers) {
let clv = 0;
for (let purchase of customer.purchases) {
clv += purchase.amount * purchase.frequency;
}
totalCLV += clv;
}
return totalCLV;
This code calculates the total CLV by summing each customer's purchase amounts multiplied by their purchase frequency.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over customers and their purchases.
- How many times: Outer loop runs once per customer; inner loop runs once per purchase per customer.
The total work grows with the number of customers and the number of purchases each customer has.
| Input Size (n customers) | Approx. Operations |
|---|---|
| 10 customers, 5 purchases each | 50 operations |
| 100 customers, 5 purchases each | 500 operations |
| 1000 customers, 5 purchases each | 5000 operations |
Pattern observation: The total operations increase roughly in proportion to the number of customers times their purchases.
Time Complexity: O(n * m)
This means the time to calculate CLV grows proportionally with both the number of customers and the number of purchases per customer.
[X] Wrong: "The calculation time only depends on the number of customers, not their purchases."
[OK] Correct: Each customer's purchases must be processed, so ignoring purchase count underestimates the work needed.
Understanding how CLV calculation scales helps you explain efficiency in real marketing data tasks, showing you can think about performance as data grows.
"What if we stored total purchase amounts per customer instead of individual purchases? How would the time complexity change?"