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?"
Practice
Solution
Step 1: Understand filtering concept
Filtering means choosing only data that fits a rule or condition.Step 2: Match purpose with options
Only To select only the data that meets certain conditions describes selecting data based on conditions, which is filtering.Final Answer:
To select only the data that meets certain conditions -> Option AQuick Check:
Filtering = selecting data by condition [OK]
- Confusing filtering with changing data values
- Thinking filtering deletes all data
- Assuming filtering adds random data
Solution
Step 1: Recall IF condition syntax
A simple IF condition uses IF, THEN, and optionally ELSE to decide actions.Step 2: Compare options to standard IF syntax
Only IF condition THEN action ELSE other_action uses IF, THEN, ELSE correctly; others use wrong keywords.Final Answer:
IF condition THEN action ELSE other_action -> Option DQuick Check:
IF-THEN-ELSE syntax = IF condition THEN action ELSE other_action [OK]
- Using FOR or WHILE instead of IF for conditions
- Mixing keywords like WHEN or OTHERWISE incorrectly
- Omitting THEN or ELSE keywords
FOR each number IN [1, 2, 3, 4, 5]
IF number % 2 == 0 THEN print(number)What will be printed?
Solution
Step 1: Understand the loop and condition
The loop goes through numbers 1 to 5. The condition checks if number is even (number % 2 == 0).Step 2: Identify even numbers in the list
Even numbers are 2 and 4, so only these get printed.Final Answer:
2 4 -> Option BQuick Check:
Even numbers printed = 2 4 [OK]
- Printing all numbers instead of only even
- Confusing even and odd numbers
- Thinking no output occurs
IF score > 50
print('Pass')
ELSE IF score > 40
print('Retake')Solution
Step 1: Check IF condition syntax
In pseudocode, IF conditions usually require THEN after the condition to separate it from actions.Step 2: Identify missing THEN keywords
Both IF and ELSE IF lines lack THEN, which is needed for clarity and correctness.Final Answer:
Missing THEN keyword after IF conditions -> Option AQuick Check:
IF conditions need THEN keyword [OK]
- Assuming ELSE IF is invalid syntax
- Thinking ELSE must always be alone
- Ignoring THEN keyword requirement
Using filtering and conditional logic, which pseudocode correctly selects products priced above 10 and prints their names?
Solution
Step 1: Understand the filtering condition
We want products with price greater than 10 only.Step 2: Check each option's condition
FOR each product IN products
IF product.price > 10 THEN print(product.name) correctly uses IF product.price > 10 to filter and print names. Others use wrong conditions or syntax.Final Answer:
FOR each product IN products
IF product.price > 10 THEN print(product.name) -> Option CQuick Check:
Filter price > 10 and print names = FOR each product IN products
IF product.price > 10 THEN print(product.name) [OK]
- Using wrong comparison operators (< or ==)
- Printing all products without filtering
- Incorrect syntax like placing IF after print
Ready to prove it?
Pass the test and this pattern moves unknown
