PIXELBANKv9.1.0
Menu

Spot vs On-Demand Expected-Cost Decision

Problem Statement

Decide whether to run a batch job on cheaper but interruptible spot instances or reliable on-demand, based on expected cost including restart overhead from interruptions.

Background

On-demand cost is on_demand_rate * hours. Spot is cheaper per hour (spot_rate) but with interruption probability p per hour, each interruption wastes restart_hours of recompute. Expected spot cost is spot_rate * (hours + expected_interruptions * restart_hours), where expected_interruptions = p * hours. Recommend "spot" if its expected cost is strictly less than on-demand, else "on_demand".

Your Task

def choose_instance(hours, on_demand_rate, spot_rate, p, restart_hours):

Return "spot" or "on_demand".

Input Format

  • hours (float), on_demand_rate, spot_rate (float), p (float in [0,1]), restart_hours (float).

Output Format

  • A string.

Sample

print(choose_instance(10, 1.0, 0.3, 0.1, 2.0))

Output:

spot

Example:

Input:
print(choose_instance(10, 1.0, 0.3, 0.1, 2.0))
Output:
spot
Reasoning:
  • Calculate the expected wasted time due to interruptions by multiplying the interruption probability, total hours, and restart duration: 0.1×10×2.0=2.00.1 \times 10 \times 2.0 = 2.0 hours.
  • Determine the total effective time for the spot instance by adding the wasted time to the original job duration: 10+2.0=12.010 + 2.0 = 12.0 hours.
  • Compute the total expected cost for the spot instance by multiplying the effective time by the spot rate: 0.3×12.0=3.60.3 \times 12.0 = 3.6.
  • Compute the total cost for the on-demand instance by multiplying the original job duration by the on-demand rate: 1.0×10=10.01.0 \times 10 = 10.0.
  • Compare the two costs to make the decision; since the spot cost (3.63.6) is strictly less than the on-demand cost (10.010.0), the spot instance is recommended.
  • The final output is spot

Constraints:

  • expected_interruptions = p * hours; wasted = expected_interruptions * restart_hours.
  • spot_cost = spot_rate * (hours + wasted); on_demand_cost = on_demand_rate * hours.
  • Choose spot iff spot_cost < on_demand_cost.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.