Negative keywords for cost control in Digital Marketing - Time & Space Complexity
When using negative keywords in digital marketing, it's important to understand how the process scales as you add more keywords.
We want to know how the time to check and filter ads grows when the list of negative keywords grows.
Analyze the time complexity of the following code snippet.
for each search_query in user_searches:
for each negative_keyword in negative_keywords_list:
if negative_keyword in search_query:
exclude ad from showing
break
else:
show ad
This code checks every user search against all negative keywords to decide if an ad should be shown or blocked.
- Primary operation: Checking each search query against all negative keywords.
- How many times: For every search query, the code loops through the entire negative keywords list until a match is found or the list ends.
As the number of negative keywords grows, the time to check each search query grows roughly in proportion.
| Input Size (number of negative keywords) | Approx. Operations per search query |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows linearly as the negative keywords list grows.
Time Complexity: O(n)
This means the time to filter ads grows directly in proportion to the number of negative keywords.
[X] Wrong: "Adding more negative keywords won't affect how fast ads are filtered because computers are fast."
[OK] Correct: Even though computers are fast, checking more keywords takes more time, so performance slows down as the list grows.
Understanding how filtering scales with negative keywords helps you design efficient ad campaigns and shows you can think about performance in real marketing systems.
"What if we used a special data structure like a hash set for negative keywords? How would the time complexity change?"