Use cases - Time & Space Complexity
When we look at use cases in programming, we want to know how the time a program takes changes as the input grows.
We ask: How does the program's work increase when we give it more data?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
void printNumbers(int n) {
for (int i = 0; i < n; i++) {
printf("%d\n", i);
}
}
int main() {
printNumbers(5);
return 0;
}
This code prints numbers from 0 up to n-1, showing a simple use case of a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints each number.
- How many times: It runs exactly n times, once for each number from 0 to n-1.
As n grows, the number of print operations grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with the input size; double the input, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the size of the input.
[X] Wrong: "The loop runs faster because it just prints numbers quickly."
[OK] Correct: The speed of printing doesn't change the fact that the loop runs once per input item, so time still grows with n.
Understanding how simple loops grow with input size helps you explain your code clearly and shows you know how programs behave as data grows.
"What if we added a nested loop inside the existing loop? How would the time complexity change?"
Practice
switch statement in C programming?Solution
Step 1: Understand the role of
Theswitchswitchstatement is used to select one of many code blocks to execute based on a variable's value.Step 2: Compare with other options
Declaring variables, repeating a block of code multiple times, and defining a function are not the purpose ofswitch.Final Answer:
To choose between multiple actions based on the value of a variable -> Option BQuick Check:
switchselects code by value [OK]
- Confusing switch with loops
- Thinking switch declares variables
- Using switch to define functions
switch statement in C?Solution
Step 1: Recall correct
The correct syntax requires the keywordswitchsyntaxswitchfollowed by parentheses enclosing the variable, then an opening brace.Step 2: Check each option
switch(variable) { matches the correct syntax:switch(variable) {. Others miss parentheses or use wrong braces.Final Answer:
switch(variable) { -> Option AQuick Check:
Syntax needs parentheses around variable [OK]
- Omitting parentheses around variable
- Using wrong braces or brackets
- Adding parentheses after variable name
int x = 2;
switch(x) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Other\n");
}Solution
Step 1: Identify the value of variable
The variablexxis set to 2.Step 2: Match
Thexwithcaselabelsswitchchecks cases: 1 and 2. Sincexis 2, it matchescase 2and prints "Two".Final Answer:
Two -> Option CQuick Check:
Value 2 matches case 2 [OK]
- Ignoring break and falling through
- Choosing default when case matches
- Confusing case numbers
switch statement:int n = 3;
switch(n) {
case 1:
printf("One\n");
case 2:
printf("Two\n");
break;
default:
printf("Default\n");
}Solution
Step 1: Check for
Case 1 lacks abreakstatementsbreak, so after printing "One", execution continues to case 2.Step 2: Understand consequences of missing
This fall-through causes both "One" and "Two" to print whenbreaknis 1, which is usually unintended.Final Answer:
Missingbreakafter case 1 causes fall-through -> Option AQuick Check:
Always add break to prevent fall-through [OK]
- Forgetting break after cases
- Thinking default must be first
- Believing switch can't use integers
switch statement that prints "Weekday" for cases 1 to 5, "Weekend" for 6 and 7, and "Invalid" for any other number. Which code snippet correctly implements this?Solution
Step 1: Understand valid
Cases can be grouped by listing them one after another without breaks to share code.switchsyntax for multiple casesStep 2: Evaluate each option
switch(day) { case 1: case 2: case 3: case 4: case 5: printf("Weekday\n"); break; case 6: case 7: printf("Weekend\n"); break; default: printf("Invalid\n"); } correctly groups cases 1 to 5 and 6 to 7, with breaks and a default. switch(day) { case 1: printf("Weekday\n"); case 2: printf("Weekday\n"); case 3: printf("Weekday\n"); case 4: printf("Weekday\n"); case 5: printf("Weekday\n"); case 6: printf("Weekend\n"); case 7: printf("Weekend\n"); default: printf("Invalid\n"); } lacks breaks causing fall-through. switch(day) { case 1 ... 5: printf("Weekday\n"); break; case 6 ... 7: printf("Weekend\n"); break; default: printf("Invalid\n"); } uses invalid syntax (case 1 ... 5). switch(day) { if(day >= 1 && day <= 5) { printf("Weekday\n"); } else if(day == 6 || day == 7) { printf("Weekend\n"); } else { printf("Invalid\n"); } } usesifinsideswitchwithout case labels, making the body unreachable and invalid for switch logic.Final Answer:
Groups cases by listing consecutive cases for shared execution with breaks -> Option DQuick Check:
Group cases by listing them, add breaks [OK]
- Using invalid range syntax in case
- Missing breaks causing fall-through
- Trying to use if inside switch
Ready to prove it?
Pass the test and this pattern moves unknown
