Viral loop design in Digital Marketing - Time & Space Complexity
When designing a viral loop, it's important to understand how the number of users grows as more people share your product.
We want to know how the effort or actions needed increase as the user base expands.
Analyze the time complexity of the following viral loop process.
# Start with 1 user
users = ["User1"]
for day in range(3):
new_users = []
next_id = len(users) + 1
for user in users:
# Each user invites 2 new users
new_users.extend([f"User{next_id + i}" for i in range(2)])
next_id += 2
users.extend(new_users)
This code simulates a viral loop where each user invites two new users every day for 3 days.
Identify the loops and repeated actions.
- Primary operation: Looping over all current users to invite new users.
- How many times: This happens once per day, repeated for 3 days, and the number of users grows each day.
Each day, the number of users roughly triples because each user invites two new users.
| Day (n) | Approx. Users |
|---|---|
| 1 | 3 (1 original + 2 new) |
| 2 | 9 (3 + 6 new) |
| 3 | 27 (9 + 18 new) |
Pattern observation: The number of users grows exponentially, roughly tripling each day in this example.
Time Complexity: O(2^n)
This means the number of operations grows very fast, doubling or more each time the loop runs.
[X] Wrong: "The number of new users grows linearly with time because each day adds a fixed number of users."
[OK] Correct: Each user invites multiple new users, so the total grows much faster than just adding a fixed amount each day.
Understanding viral loops helps you explain how user growth can explode quickly and why managing that growth matters in real projects.
"What if each user invited only one new user instead of two? How would the time complexity change?"