0
0
GCPcloud~5 mins

Compute commands (instances, disks) in GCP - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Compute commands (instances, disks)
O(n)
Understanding Time Complexity

When using compute commands to manage instances and disks, it's important to know how the number of commands grows as you manage more resources.

We want to understand how the total work changes when we increase the number of instances or disks.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.

for instance in instances:
  gcloud compute instances start $instance

for disk in disks:
  gcloud compute disks create $disk --size=100GB

for instance in instances:
  gcloud compute instances attach-disk $instance --disk=${instance}-disk

This sequence starts multiple instances, creates multiple disks, and attaches disks to instances one by one.

Identify Repeating Operations

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

  • Primary operation: Starting instances, creating disks, attaching disks
  • How many times: Each operation runs once per instance or disk, so the number of calls grows with the number of resources.
How Execution Grows With Input

As you add more instances and disks, the number of commands grows roughly in direct proportion.

Input Size (n)Approx. Api Calls/Operations
10About 30 (10 starts + 10 creates + 10 attaches)
100About 300 (100 starts + 100 creates + 100 attaches)
1000About 3000 (1000 starts + 1000 creates + 1000 attaches)

Pattern observation: The total operations increase linearly as the number of instances and disks increase.

Final Time Complexity

Time Complexity: O(n)

This means the total commands grow directly with the number of instances and disks you manage.

Common Mistake

[X] Wrong: "Starting or creating multiple resources happens all at once, so time stays the same no matter how many."

[OK] Correct: Each command runs separately, so more resources mean more commands and more time overall.

Interview Connect

Understanding how command counts grow helps you plan and explain resource management clearly in real projects.

Self-Check

"What if we attached multiple disks to each instance instead of one? How would the time complexity change?"