Filtering and conditional logic in No-Code - Time & Space Complexity
When we filter items using conditions, the time it takes depends on how many items we check.
We want to know how the work grows as the list gets bigger.
Analyze the time complexity of the following code snippet.
filtered_items = []
for item in items:
if item > 10:
filtered_items.append(item)
This code goes through a list and keeps only the items greater than 10.
- Primary operation: Looping through each item in the list once.
- How many times: Exactly once for every item in the list.
As the list gets bigger, the code checks each item one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of items.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the list gets bigger.
[X] Wrong: "Filtering only a few items means the code runs faster than checking all items."
[OK] Correct: The code still looks at every item to decide if it matches, so it always checks all items no matter how many pass the condition.
Understanding how filtering scales helps you explain how your code handles bigger data smoothly and shows you can think about efficiency clearly.
"What if we used two nested loops to filter items based on pairs? How would the time complexity change?"