Referral program mechanics in Digital Marketing - Time & Space 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.
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.
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.
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 each | About 30 operations (10 invites + 20 rewards/notifications) |
| 100 users with 2 referrals each | About 300 operations |
| 1000 users with 2 referrals each | About 3000 operations |
Pattern observation: The total operations increase roughly in direct proportion to the number of users and their referrals.
Time Complexity: O(n)
This means the work grows linearly with the number of users and their referrals combined.
[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.
Understanding how referral program operations grow helps you design scalable marketing strategies and shows you can think about efficiency in real-world systems.
"What if each user could have a varying number of referrals instead of a fixed number? How would the time complexity change?"