For loop over collections in Kotlin - Time & Space Complexity
When we use a for loop to go through a collection, we want to know how the time it takes changes as the collection grows.
We ask: How does the work increase when the list gets bigger?
Analyze the time complexity of the following code snippet.
fun printAllNames(names: List) {
for (name in names) {
println(name)
}
}
This code goes through each name in the list and prints it out one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop that visits each item in the list.
- How many times: Exactly once for each item in the list.
As the list gets bigger, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print actions |
| 100 | 100 print actions |
| 1000 | 1000 print actions |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the size of the list.
[X] Wrong: "The loop runs faster because it just prints simple text."
[OK] Correct: The speed depends on how many times the loop runs, not what it does inside. Even simple actions add up when repeated many times.
Understanding how loops grow with input size helps you explain your code clearly and shows you know how programs behave as data grows.
"What if we nested another for loop inside this one to print pairs of names? How would the time complexity change?"
Practice
for loop do when used with a collection in Kotlin?Solution
Step 1: Understand the purpose of a for loop
A for loop runs code repeatedly for each element in a collection like a list or array.Step 2: Identify what happens in the loop
Each item is accessed one by one, and the code inside the loop runs for each item.Final Answer:
It repeats a block of code for each item in the collection. -> Option DQuick Check:
For loop = repeat for each item [OK]
- Thinking for loop creates or removes items
- Confusing for loop with sorting or filtering
- Believing for loop runs only once
for loop over a list named items in Kotlin?Solution
Step 1: Recall Kotlin for loop syntax
Kotlin requires parentheses around the loop variable and collection: for (item in items) { ... }Step 2: Check each option
for (item in items) { println(item) } matches the correct syntax with parentheses and braces.Final Answer:
for (item in items) { println(item) } -> Option BQuick Check:
Parentheses + in keyword = correct syntax [OK]
- Omitting parentheses around loop header
- Using colon ':' instead of 'in'
- Missing braces for loop body
val numbers = listOf(1, 2, 3)
for (num in numbers) {
print(num * 2)
}Solution
Step 1: Understand the loop and list
The list contains 1, 2, 3. The loop goes through each number.Step 2: Calculate output for each iteration
Each number is multiplied by 2 and printed without spaces: 1*2=2, 2*2=4, 3*2=6, so output is "246".Final Answer:
246 -> Option AQuick Check:
Print doubles of 1,2,3 = 246 [OK]
- Assuming print adds spaces or newlines
- Confusing multiplication with addition
- Expecting a list output instead of concatenated string
val fruits = listOf("apple", "banana", "cherry")
for fruit in fruits {
println(fruit)
}Solution
Step 1: Check for correct for loop syntax
Kotlin requires parentheses around the loop header: for (fruit in fruits) { ... }Step 2: Identify the missing parentheses
The code misses parentheses around 'fruit in fruits', causing a syntax error.Final Answer:
Missing parentheses around loop header -> Option AQuick Check:
Parentheses required in Kotlin for loops [OK]
- Omitting parentheses in for loop
- Using wrong loop variable syntax
- Assuming print statement causes error
for loop to print only names longer than 4 characters?
Choose the correct code snippet.Solution
Step 1: Understand the condition to filter names
We want to print names only if their length is greater than 4 characters.Step 2: Check each option for correct syntax and logic
for (name in names) { if (name.length > 4) println(name) } correctly uses an if statement inside the loop to check length and print the name.Final Answer:
for (name in names) { if (name.length > 4) println(name) } -> Option CQuick Check:
Use if inside for to filter by length [OK]
- Comparing string directly to number
- Printing boolean instead of name
- Incorrect syntax in if or print statements
Ready to prove it?
Pass the test and this pattern moves unknown
