0
0
Cybersecurityknowledge~5 mins

Malware types (virus, worm, trojan, ransomware) in Cybersecurity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Malware types (virus, worm, trojan, ransomware)
O(n²)
Understanding Time Complexity

When studying malware types, it helps to understand how their actions grow as they spread or infect systems.

We want to know how the effort or damage increases as more devices or files are involved.

Scenario Under Consideration

Analyze the time complexity of this simplified malware spreading code.


for infected_device in network:
    for connected_device in infected_device.connections:
        if connected_device.is_vulnerable():
            infect(connected_device)

This code shows how a malware worm spreads by trying to infect connected devices in a network.

Identify Repeating Operations
  • Primary operation: Checking and infecting connected devices.
  • How many times: For each infected device, it checks all its connections.
How Execution Grows With Input

As the number of infected devices grows, the number of checks grows too.

Input Size (n)Approx. Operations
10About 100 checks
100About 10,000 checks
1000About 1,000,000 checks

Pattern observation: The work grows very fast as more devices get infected, roughly multiplying by itself.

Final Time Complexity

Time Complexity: O(n²)

This means the effort to spread grows roughly with the square of the number of infected devices.

Common Mistake

[X] Wrong: "The malware only does a fixed amount of work no matter how many devices are infected."

[OK] Correct: As more devices get infected, the malware tries to infect more connections, so the work grows quickly.

Interview Connect

Understanding how malware spreads helps you think about how systems can be protected and how attacks scale in real life.

Self-Check

"What if the malware only tried to infect one new device per infected device instead of all connections? How would the time complexity change?"