0
0
Digital Marketingknowledge~5 mins

Negative keywords for cost control in Digital Marketing - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Negative keywords for cost control
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations
  • 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.
How Execution Grows With Input

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
10About 10 checks
100About 100 checks
1000About 1000 checks

Pattern observation: The number of checks grows linearly as the negative keywords list grows.

Final Time Complexity

Time Complexity: O(n)

This means the time to filter ads grows directly in proportion to the number of negative keywords.

Common Mistake

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

Interview Connect

Understanding how filtering scales with negative keywords helps you design efficient ad campaigns and shows you can think about performance in real marketing systems.

Self-Check

"What if we used a special data structure like a hash set for negative keywords? How would the time complexity change?"