Instance states (running, stopped, terminated) in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to check or change instance states grows as we handle more instances.
How does the number of instances affect the work done by the system?
Analyze the time complexity of the following operation sequence.
# List all instances in a project
instances = compute.instances().list(project=project_id, zone=zone).execute()
# For each instance, check its state
for instance in instances['items']:
state = instance['status']
if state == 'TERMINATED':
# Optionally delete or ignore
pass
elif state == 'STOPPED':
# Optionally start instance
pass
elif state == 'RUNNING':
# Optionally stop instance
pass
This code lists all instances and checks their current state to decide what action to take.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Checking each instance's state via the list API and iterating over each instance.
- How many times: Once per instance in the project and zone.
As the number of instances grows, the number of state checks grows at the same rate.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 state checks |
| 100 | 100 state checks |
| 1000 | 1000 state checks |
Pattern observation: The work grows linearly with the number of instances.
Time Complexity: O(n)
This means the time to check or act on instance states grows directly with how many instances there are.
[X] Wrong: "Checking all instance states takes the same time no matter how many instances exist."
[OK] Correct: Each instance adds one more check, so more instances mean more work and longer time.
Understanding how operations scale with instance count helps you design efficient cloud management scripts and shows you think about system behavior as it grows.
"What if we batch instance state checks instead of checking one by one? How would the time complexity change?"
Practice
RUNNING state of a Google Cloud VM instance indicate?Solution
Step 1: Understand VM states
The RUNNING state means the virtual machine is powered on and operational.Step 2: Compare with other states
TERMINATED means stopped but restartable; DELETED means removed permanently.Final Answer:
The VM is active and ready to use -> Option CQuick Check:
RUNNING = active and ready [OK]
- Confusing TERMINATED with RUNNING
- Thinking DELETED means stopped
- Assuming RUNNING means VM is paused
my-vm using gcloud CLI?Solution
Step 1: Identify the correct stop command
The command to stop a VM isgcloud compute instances stop.Step 2: Check other commands
deleteremoves the VM,terminateis not a valid gcloud command,suspendpreserves memory state but is different from stop.Final Answer:
gcloud compute instances stop my-vm -> Option AQuick Check:
Stop VM = gcloud compute instances stop [OK]
- Using delete instead of stop
- Typing terminate which is invalid
- Confusing suspend with stop
NAME ZONE STATUS vm-1 us-central1-a TERMINATED vm-2 us-central1-a RUNNING vm-3 us-central1-a TERMINATED
Which VM(s) can be restarted without creating a new instance?
Solution
Step 1: Understand TERMINATED state
TERMINATED means the VM is stopped but can be restarted later.Step 2: Identify which VMs are TERMINATED
vm-1 and vm-3 are TERMINATED, so they can be restarted; vm-2 is already RUNNING.Final Answer:
vm-1 and vm-3 -> Option BQuick Check:
TERMINATED = restartable VMs [OK]
- Thinking RUNNING VMs need restart
- Assuming TERMINATED means deleted
- Selecting all VMs regardless of state
Solution
Step 1: Analyze error message
Error 'instance does not exist' means the VM resource is gone.Step 2: Match with VM states
Only DELETED means the VM is removed permanently; RUNNING, TERMINATED, SUSPENDED still exist.Final Answer:
The VM was DELETED and no longer exists -> Option AQuick Check:
Deleted VM = does not exist error [OK]
- Confusing TERMINATED with DELETED
- Assuming STOPPED means deleted
- Ignoring error message meaning
Solution
Step 1: Understand cost-saving states
Stopping a VM saves costs but keeps data if the VM is TERMINATED.Step 2: Evaluate options
RUNNING uses full resources; DELETED removes VM and data; SUSPENDED preserves memory but standard stop uses TERMINATED.Final Answer:
TERMINATED, because it stops the VM but preserves data and allows restart -> Option DQuick Check:
Stop VM = TERMINATED state for cost saving [OK]
- Choosing RUNNING to save costs
- Deleting VM to save costs but losing data
- Choosing SUSPENDED instead of TERMINATED
