0
0
Digital Marketingknowledge~5 mins

Referral program mechanics in Digital Marketing - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Referral program mechanics
O(n)
Understanding Time Complexity

When running a referral program, it's important to understand how the effort or cost grows as more users join and refer others.

We want to know how the number of operations changes as the number of participants increases.

Scenario Under Consideration

Analyze the time complexity of the following referral program process.


// For each new user:
for each user in new_users:
    send_invite(user)
    for each referral in user.referrals:
        reward_user(referral)
        notify_user(referral)
    update_program_stats()
    

This code sends invites to new users, rewards and notifies their referrals, and updates stats.

Identify Repeating Operations

Look at the loops and repeated actions:

  • Primary operation: Looping through each new user and then through each referral they bring.
  • How many times: The outer loop runs once per new user; the inner loop runs once per referral of that user.
How Execution Grows With Input

As the number of new users and their referrals grow, the total work grows too.

Input Size (n)Approx. Operations
10 users with 2 referrals eachAbout 30 operations (10 invites + 20 rewards/notifications)
100 users with 2 referrals eachAbout 300 operations
1000 users with 2 referrals eachAbout 3000 operations

Pattern observation: The total operations increase roughly in direct proportion to the number of users and their referrals.

Final Time Complexity

Time Complexity: O(n)

This means the work grows linearly with the number of users and their referrals combined.

Common Mistake

[X] Wrong: "The program runs in constant time because each user is handled separately."

[OK] Correct: Even though users are handled one by one, the total work depends on how many users and referrals there are, so it grows as more join.

Interview Connect

Understanding how referral program operations grow helps you design scalable marketing strategies and shows you can think about efficiency in real-world systems.

Self-Check

"What if each user could have a varying number of referrals instead of a fixed number? How would the time complexity change?"