0
0
Digital Marketingknowledge~5 mins

Customer lifetime value (CLV) calculation in Digital Marketing - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Customer lifetime value (CLV) calculation
O(n * m)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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 each50 operations
100 customers, 5 purchases each500 operations
1000 customers, 5 purchases each5000 operations

Pattern observation: The total operations increase roughly in proportion to the number of customers times their purchases.

Final Time Complexity

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.

Common Mistake

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

Interview Connect

Understanding how CLV calculation scales helps you explain efficiency in real marketing data tasks, showing you can think about performance as data grows.

Self-Check

"What if we stored total purchase amounts per customer instead of individual purchases? How would the time complexity change?"