Vararg parameters in Kotlin - Time & Space Complexity
We want to understand how the time to run a function changes when it accepts a variable number of arguments.
How does the function's work grow as we give it more inputs?
Analyze the time complexity of the following code snippet.
fun sumAll(vararg numbers: Int): Int {
var sum = 0
for (num in numbers) {
sum += num
}
return sum
}
This function adds up all the numbers passed to it using a vararg parameter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the vararg array.
- How many times: Once for each number passed to the function.
As you give more numbers, the function adds each one, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of inputs.
Time Complexity: O(n)
This means the time to run the function grows in a straight line with the number of arguments.
[X] Wrong: "Vararg parameters make the function run instantly no matter how many inputs."
[OK] Correct: The function still processes each input one by one, so more inputs mean more work.
Understanding how vararg parameters affect time helps you explain how flexible functions handle many inputs efficiently.
"What if we changed the function to call itself recursively for each number? How would the time complexity change?"
Practice
What does the vararg keyword do in Kotlin functions?
Solution
Step 1: Understand the purpose of
Thevarargvarargkeyword lets a function accept zero or more arguments of the same type without needing an array or list explicitly.Step 2: Compare options with this meaning
Only Allows passing a variable number of arguments to a function correctly describes this behavior, while others describe unrelated function features.Final Answer:
Allows passing a variable number of arguments to a function -> Option AQuick Check:
Vararg = variable arguments [OK]
- Thinking vararg returns multiple values
- Confusing vararg with no parameters
- Assuming vararg limits to one argument
Which of the following is the correct syntax to declare a function with a vararg parameter in Kotlin?
fun printAll(____ numbers: Int) { for (n in numbers) println(n) }Solution
Step 1: Recall the exact keyword for variable arguments
Kotlin usesvararg(singular) to declare variable number of arguments.Step 2: Check each option
Only vararg matches the correct keyword; others are incorrect or invalid syntax.Final Answer:
vararg -> Option CQuick Check:
Keyword is vararg, not varargs [OK]
- Using 'varargs' instead of 'vararg'
- Omitting the keyword entirely
- Using 'var' or 'args' which are invalid here
What is the output of this Kotlin code?
fun sumAll(vararg numbers: Int): Int { var sum = 0; for (n in numbers) sum += n; return sum }
fun main() {
println(sumAll(1, 2, 3))
println(sumAll())
}Solution
Step 1: Understand the function behavior with vararg
The function sums all integers passed. If no arguments, the loop does not run, sum stays 0.Step 2: Calculate outputs for calls
sumAll(1,2,3) = 1+2+3 = 6; sumAll() = 0 (no arguments)Final Answer:
6 and 0 -> Option DQuick Check:
Sum of inputs or zero if none [OK]
- Assuming empty vararg causes error
- Forgetting sum starts at zero
- Confusing vararg with list requiring explicit array
Identify the error in this Kotlin function declaration:
fun printNames(vararg names: String, age: Int) {
for (name in names) println(name)
println(age)
}Solution
Step 1: Recall vararg parameter position rule
Kotlin requires the vararg parameter to be the last in the parameter list.Step 2: Check the function parameters
Here, varargnamesis beforeage, which is invalid syntax.Final Answer:
Vararg parameter must be the last parameter -> Option BQuick Check:
Vararg last parameter rule [OK]
- Placing vararg parameter before others
- Thinking vararg can be anywhere
- Using 'varargs' instead of 'vararg'
Given this function:
fun combine(vararg parts: String): String {
return parts.joinToString(separator = ", ")
}Which call correctly passes an existing array val arr = arrayOf("a", "b", "c") to combine?
Solution
Step 1: Understand how to pass an array to vararg
Kotlin requires the spread operator*to pass an array as vararg arguments.Step 2: Evaluate each option
combine(*arr) uses*arr, correctly spreading the array. combine(arr) passes the array as a single argument, not vararg. combine(arr.toList()) passes a list, not vararg. combine(arr as String) is invalid cast.Final Answer:
combine(*arr) -> Option AQuick Check:
Use * to spread array for vararg [OK]
- Passing array without * operator
- Passing list instead of array
- Trying invalid casts to String
Ready to prove it?
Pass the test and this pattern moves unknown
