Search intent matching in SEO Fundamentals - Time & Space Complexity
When matching search intent, we want to see how the work grows as more queries or keywords come in.
How does the effort to find the right intent change when the input grows?
Analyze the time complexity of the following search intent matching process.
// For each user query
for each query in queries:
// Check each intent in the intent list
for each intent in intents:
if query matches intent:
return intent
// If no match found, return default
return default_intent
This code checks each user query against a list of possible intents until it finds a match.
Look at what repeats as input grows.
- Primary operation: Checking each query against each intent.
- How many times: For every query, it loops through all intents until a match is found.
As the number of queries or intents grows, the checks increase.
| Input Size (queries x intents) | Approx. Operations |
|---|---|
| 10 queries x 5 intents | About 50 checks |
| 100 queries x 5 intents | About 500 checks |
| 1000 queries x 5 intents | About 5000 checks |
Pattern observation: The total checks grow roughly by multiplying queries and intents.
Time Complexity: O(n x m)
This means the work grows by multiplying the number of queries (n) by the number of intents (m).
[X] Wrong: "Matching search intent takes the same time no matter how many queries or intents there are."
[OK] Correct: More queries or intents mean more checks, so the time needed grows with input size.
Understanding how matching scales helps you explain how your solution handles many users or intents efficiently.
"What if we used a fast lookup method like a hash map for intents? How would the time complexity change?"