Data-driven budget allocation in Digital Marketing - Time & Space Complexity
When allocating budgets based on data, it is important to understand how the time to calculate allocations grows as the number of campaigns increases.
We want to know how the work needed changes when more campaigns or data points are involved.
Analyze the time complexity of the following code snippet.
// Assume campaigns is a list of campaign data
// totalBudget is the total amount to allocate
function allocateBudget(campaigns, totalBudget) {
let totalScore = 0;
for (let campaign of campaigns) {
totalScore += campaign.performanceScore;
}
let allocations = [];
for (let campaign of campaigns) {
let share = (campaign.performanceScore / totalScore) * totalBudget;
allocations.push({campaignId: campaign.id, budget: share});
}
return allocations;
}
This code calculates the total performance score of all campaigns, then allocates the budget proportionally to each campaign based on its score.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops over the list of campaigns.
- How many times: Each loop runs once over all campaigns, so twice in total.
As the number of campaigns increases, the time to process grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (2 loops x 10 campaigns) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: Doubling the number of campaigns roughly doubles the work needed.
Time Complexity: O(n)
This means the time to allocate budget grows linearly with the number of campaigns.
[X] Wrong: "Because there are two loops, the time complexity is squared (O(n²))."
[OK] Correct: The loops run one after another, not nested inside each other, so their times add up, not multiply.
Understanding how time grows with input size helps you explain your approach clearly and shows you can think about efficiency in real-world marketing tools.
"What if the budget allocation was done inside a nested loop comparing each campaign to every other campaign? How would the time complexity change?"