Risk analysis (probability and impact) in Software Engineering - Time & Space Complexity
When analyzing risk, we want to understand how likely a problem is and how big its effect could be.
We ask: How does the chance and impact of risks grow as we consider more factors?
Analyze the time complexity of the following risk scoring process.
risks = [ ... ] # list of risk items
for risk in risks:
probability = risk.getProbability()
impact = risk.getImpact()
riskScore = probability * impact
recordRiskScore(risk, riskScore)
This code calculates a risk score for each risk by multiplying its probability and impact.
Look for repeated actions that take time.
- Primary operation: Looping through each risk item once.
- How many times: Once per risk, so as many times as there are risks.
As the number of risks increases, the work grows in a simple way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calculations |
| 100 | 100 calculations |
| 1000 | 1000 calculations |
Pattern observation: The work grows directly with the number of risks.
Time Complexity: O(n)
This means the time to calculate risk scores grows in a straight line as the number of risks grows.
[X] Wrong: "Calculating risk scores takes the same time no matter how many risks there are."
[OK] Correct: Each risk needs its own calculation, so more risks mean more work.
Understanding how risk calculations scale helps you explain how your code handles growing data clearly and confidently.
"What if we had to compare every risk with every other risk? How would the time complexity change?"