Data quality flags in SCADA systems - Time & Space Complexity
We want to understand how the time to check data quality flags changes as we handle more data points.
How does the work grow when we have more sensor readings to check?
Analyze the time complexity of the following code snippet.
for reading in sensor_readings:
if reading.value < 0 or reading.value > reading.max_limit:
reading.flag = 'error'
else:
reading.flag = 'ok'
This code checks each sensor reading and sets a flag based on whether the value is within limits.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each sensor reading once.
- How many times: Once for every reading in the list.
As the number of readings increases, the time to check flags grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: Doubling the readings doubles the work needed.
Time Complexity: O(n)
This means the time to set flags grows in a straight line with the number of readings.
[X] Wrong: "Checking flags is always fast and constant time no matter how many readings there are."
[OK] Correct: Each reading must be checked individually, so more readings mean more work.
Understanding how work grows with data size helps you explain system behavior clearly and shows you can think about efficiency in real tasks.
"What if we added nested checks inside the loop for multiple conditions? How would the time complexity change?"