0
0
Digital Marketingknowledge~5 mins

Viral loop design in Digital Marketing - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Viral loop design
O(2^n)
Understanding Time 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.

Scenario Under Consideration

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

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

Each day, the number of users roughly triples because each user invites two new users.

Day (n)Approx. Users
13 (1 original + 2 new)
29 (3 + 6 new)
327 (9 + 18 new)

Pattern observation: The number of users grows exponentially, roughly tripling each day in this example.

Final Time Complexity

Time Complexity: O(2^n)

This means the number of operations grows very fast, doubling or more each time the loop runs.

Common Mistake

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

Interview Connect

Understanding viral loops helps you explain how user growth can explode quickly and why managing that growth matters in real projects.

Self-Check

"What if each user invited only one new user instead of two? How would the time complexity change?"