0
0
Azurecloud~5 mins

Scripting with variables and loops in Azure - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Scripting with variables and loops
O(n)
Understanding Time Complexity

When writing scripts in Azure, it is important to know how the time taken grows as you repeat tasks using loops and variables.

We want to understand how the number of repeated actions affects the total time to complete the script.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.

for i in range(1, n + 1):
    resource_name = f"resource-{i}"
    az resource create --name "$resource_name" --resource-type "Microsoft.Storage/storageAccounts" --location eastus

This script creates n storage accounts in Azure, naming each uniquely using a variable inside a loop.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: The command to create a storage account using Azure CLI.
  • How many times: This command runs once for each value of i, so n times.
How Execution Grows With Input

Each new storage account requires one create command, so the total commands grow directly with n.

Input Size (n)Approx. Api Calls/Operations
1010 create commands
100100 create commands
10001000 create commands

Pattern observation: The number of operations grows in a straight line as n increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the script grows directly in proportion to the number of resources you create.

Common Mistake

[X] Wrong: "Running the create command inside a loop will take the same time no matter how many resources I create."

[OK] Correct: Each create command takes time and runs separately, so more resources mean more total time.

Interview Connect

Understanding how loops affect execution time helps you write efficient scripts and explain your reasoning clearly in real-world cloud tasks.

Self-Check

"What if we created multiple resources in a single command instead of one per loop? How would the time complexity change?"