Repeat function for simple repetition in Kotlin - Time & Space Complexity
We want to understand how the time taken by Kotlin's repeat function changes as we repeat an action more times.
The question is: How does the number of repetitions affect the total work done?
Analyze the time complexity of the following code snippet.
fun printHello(times: Int) {
repeat(times) {
println("Hello")
}
}
printHello(5)
This code prints "Hello" a number of times given by times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
repeatfunction runs the print statement multiple times. - How many times: Exactly
timestimes, as given by the input.
Each time we increase times, the print action happens that many more times.
| Input Size (times) | Approx. Operations (prints) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means if you double the number of repetitions, the total work roughly doubles too.
[X] Wrong: "The repeat function runs instantly no matter how many times."
[OK] Correct: Each repetition runs the code inside once, so more repetitions mean more work and more time.
Understanding how simple loops like repeat scale helps you explain how programs handle repeated tasks efficiently.
"What if we nested one repeat inside another? How would the time complexity change?"
Practice
repeat function do?Solution
Step 1: Understand the purpose of
Therepeatrepeatfunction is designed to run a block of code a fixed number of times.Step 2: Compare with other options
Options B and C describe string or list repetition, which are different functions. Defines a loop that runs until a condition is false describes a conditional loop, notrepeat.Final Answer:
Runs a block of code a specified number of times -> Option CQuick Check:
repeat = run code N times [OK]
- Confusing repeat with string repetition
- Thinking repeat creates collections
- Mixing repeat with while or for loops
Solution
Step 1: Check the function call syntax
Therepeatfunction requires parentheses around the number of repetitions and a lambda block with curly braces.Step 2: Validate each option
repeat(3) { println("Hello") } correctly usesrepeat(3) { ... }. Options B, C, and D have syntax errors or misplaced braces.Final Answer:
repeat(3) { println("Hello") } -> Option BQuick Check:
repeat(number) { code } syntax [OK]
- Omitting parentheses around the count
- Using semicolon instead of braces
- Placing braces incorrectly
repeat(4) { i -> println(i * 2) }Solution
Step 1: Understand the index parameter
The lambda receivesifrom 0 to 3 (4 times). Each iteration printsi * 2.Step 2: Calculate each output line
For i=0: 0*2=0, i=1: 2, i=2: 4, i=3: 6. So output lines are 0, 2, 4, 6.Final Answer:
0 2 4 6 -> Option DQuick Check:
repeat with index * 2 = 0 2 4 6 [OK]
- Starting index at 1 instead of 0
- Forgetting to multiply by 2
- Confusing output with index values
repeat(3)
println("Hi")Solution
Step 1: Check the syntax of repeat
Therepeatfunction expects a lambda block enclosed in curly braces after the count.Step 2: Identify the missing braces
The code has no braces aroundprintln("Hi"), so it is a syntax error.Final Answer:
Missing curly braces for the code block -> Option AQuick Check:
repeat needs braces { } for code [OK]
- Omitting braces for single statement
- Adding semicolon after number
- Passing number as string
repeat to create a list of squares of numbers from 0 to 4 in Kotlin?Solution
Step 1: Understand repeat usage for list building
Therepeatfunction runs code multiple times but does not return a list itself.Step 2: Check each option's approach
val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) } creates a mutable list and adds squares inside repeat, which works. val squares = repeat(5) { it * it } tries to assign repeat's result directly, which is Unit (no list). val squares = List(5) { repeat(it * it) } misuses List and repeat. val squares = repeat(5) { println(it * it) } only prints values, no list created.Final Answer:
val squares = mutableListOf<Int>(); repeat(5) { squares.add(it * it) } -> Option AQuick Check:
repeat + add to list = correct list [OK]
- Expecting repeat to return a list
- Using repeat inside List constructor incorrectly
- Only printing values without storing
Ready to prove it?
Pass the test and this pattern moves unknown
