Building an email list in Digital Marketing - Time & Space Complexity
When building an email list, it's important to understand how the time needed grows as you add more contacts.
We want to know how the effort or steps increase when the list gets bigger.
Analyze the time complexity of the following process to add new emails to a list.
// Pseudocode for adding emails to a list
for each email in newEmails:
if email not in existingList:
add email to existingList
This code checks each new email and adds it only if it is not already in the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking if each new email is already in the existing list.
- How many times: Once for each new email, and for each check, it may scan the existing list.
As the number of new emails grows, the time to check each one grows too, especially if the existing list is large.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks (10 new emails x 10 existing emails) |
| 100 | About 10,000 checks (100 new emails x 100 existing emails) |
| 1000 | About 1,000,000 checks (1000 new emails x 1000 existing emails) |
Pattern observation: The number of checks grows much faster than the number of new emails because each new email is compared against all existing ones.
Time Complexity: O(n × m)
This means the time grows roughly by multiplying the number of new emails (n) by the size of the existing list (m).
[X] Wrong: "Adding emails one by one always takes the same time no matter how big the list is."
[OK] Correct: Each new email must be checked against the whole existing list, so as the list grows, the checking takes more time.
Understanding how adding items to a list scales helps you design better marketing tools and shows you can think about efficiency in real projects.
"What if we used a special data structure that lets us check if an email exists instantly? How would the time complexity change?"