Data compression techniques in SCADA systems - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we compress data in SCADA systems, we want to know how the time to compress grows as the data size grows.
We ask: How much longer does compression take if the data doubles or triples?
Analyze the time complexity of the following code snippet.
function compressData(data) {
let compressed = []
for (let i = 0; i < data.length; i++) {
let count = 1
while (i + 1 < data.length && data[i] === data[i + 1]) {
count++
i++
}
compressed.push({value: data[i], count: count})
}
return compressed
}
This code compresses data by counting repeated values in a row and storing the value with its count.
- Primary operation: The outer for-loop goes through each data item once.
- How many times: The inner while-loop counts repeated items, but together they cover the whole data once.
As data size grows, the loops still process each item once, so time grows steadily with data size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The time to compress grows roughly in direct proportion to the data size.
Time Complexity: O(n)
This means the time to compress grows linearly as the data size grows.
[X] Wrong: "The inner while-loop makes this code take much longer than the data size."
[OK] Correct: The inner loop only counts repeated items and together with the outer loop, each item is processed once, so time grows linearly.
Understanding how loops work together to process data once is a key skill in analyzing performance for real-world systems like SCADA.
"What if the data was sorted before compression? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand data compression purpose
Data compression reduces the size of data to save space and speed up transfer.Step 2: Apply this to SCADA systems
In SCADA, smaller data means faster communication and less storage needed.Final Answer:
To reduce the size of data for easier storage and faster transfer -> Option AQuick Check:
Compression = smaller data size [OK]
- Confusing compression with encryption
- Thinking compression deletes data
- Believing compression changes data meaning
compress in a SCADA script?Solution
Step 1: Identify correct function call syntax
Functions are called with parentheses enclosing arguments, like compress(data).Step 2: Check each option
compressed_data = compress(data) uses correct syntax with parentheses and assignment.Final Answer:
compressed_data = compress(data) -> Option AQuick Check:
Function call syntax = parentheses [OK]
- Omitting parentheses in function calls
- Using wrong assignment operators
- Using brackets instead of parentheses
data = "sensor_reading_12345" compressed = compress(data) decompressed = decompress(compressed) print(decompressed)
What will be the output?
Solution
Step 1: Understand compression and decompression
compress() shrinks data, decompress() restores it to original form.Step 2: Follow the script flow
Data is compressed then decompressed, so print shows original data.Final Answer:
sensor_reading_12345 -> Option CQuick Check:
Decompress(compress(data)) = original data [OK]
- Thinking print shows compressed bytes
- Assuming decompress changes data
- Ignoring function order
compressed = compress(data) but later decompressed = decompress(data) is called instead of decompress(compressed). What is the likely problem?Solution
Step 1: Identify variable usage error
Decompress must use compressed data, not original data variable.Step 2: Understand effect of wrong variable
Using original data in decompress causes failure or incorrect output.Final Answer:
Decompression will fail or give wrong data because wrong variable is used -> Option DQuick Check:
Decompress(compressed) needed, not decompress(data) [OK]
- Passing original data to decompress
- Assuming decompress auto-detects input
- Mixing variable names
Solution
Step 1: Understand real-time monitoring needs
Real-time needs exact data quickly without loss.Step 2: Choose compression type
Lossless compression keeps data exact and fast to decompress.Step 3: Evaluate other options
Lossy loses data, no compression wastes space, encryption is different.Final Answer:
Lossless compression for exact data recovery -> Option BQuick Check:
Real-time + exact data = lossless compression [OK]
- Choosing lossy compression for critical data
- Skipping compression to save time
- Confusing encryption with compression
