Generic function declaration in Kotlin - Time & Space Complexity
When we write a generic function, it can work with many types of data. We want to see how the time it takes to run changes as the input size grows.
How does the function's work grow when we give it more items to handle?
Analyze the time complexity of the following code snippet.
fun <T> printAll(items: List<T>) {
for (item in items) {
println(item)
}
}
This function takes a list of any type and prints each item one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the list.
- How many times: Once for every item in the list.
As the list gets bigger, the function prints more items, so it does more work.
| 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 run grows in a straight line with the number of items in the list.
[X] Wrong: "Because the function is generic, it must be slower or more complex."
[OK] Correct: The generic part only means it works with any type, but the loop still runs once per item, so the time depends on the list size, not the type.
Understanding how generic functions behave helps you explain your code clearly and shows you know how performance depends on input size, a key skill in programming.
"What if we changed the function to print only the first half of the list? How would the time complexity change?"
Practice
<T> before the function name?Solution
Step 1: Understand generic type declaration
Declaring<T>before a function name means the function can accept any type as input.Step 2: Purpose of generic functions
This makes the function reusable for different data types without rewriting it for each type.Final Answer:
To allow the function to work with any type specified when called -> Option DQuick Check:
Generic functions = reusable for any type [OK]
- Thinking generic restricts to one type
- Confusing generic with function overloading
- Assuming generic improves speed automatically
printItem that takes a parameter of any type T in Kotlin?Solution
Step 1: Identify correct generic syntax
In Kotlin, generic type parameters are declared before the function name using<T>.Step 2: Check parameter declaration
The parameter must specify its type asT, soitem: Tis correct.Final Answer:
fun <T> printItem(item: T) { println(item) } -> Option AQuick Check:
Generic syntax = <T> before function name [OK]
- Placing <T> after function name
- Not specifying parameter type as T
- Using parentheses incorrectly around <T>
fun <T> identity(value: T): T {
return value
}
fun main() {
println(identity(123))
println(identity("Hello"))
}Solution
Step 1: Understand the generic function behavior
The functionidentityreturns the same value it receives, regardless of type.Step 2: Trace the calls in main()
Callingidentity(123)returns 123, andidentity("Hello")returns "Hello".Final Answer:
123 Hello -> Option BQuick Check:
Generic function returns input value [OK]
- Expecting type conversion
- Thinking output repeats first call
- Assuming compilation error due to mixed types
fun printValue<T>(value: T) {
println(value)
}Solution
Step 1: Check generic syntax rules
In Kotlin, generic type parameters must be declared before the function name, not after.Step 2: Analyze the given code
The code declaresprintValue<T>, which is incorrect syntax.Final Answer:
Generic type parameter <T> must be declared before the function name -> Option AQuick Check:
Generic <T> goes before function name [OK]
- Putting <T> after function name
- Assuming generic parameters go in parentheses
- Thinking generic functions must return a value
swap that swaps two elements in a mutable list at given indices. Which of the following is the correct Kotlin function declaration and implementation?Solution
Step 1: Correct generic syntax and parameter types
The generic type parameter<T>must be declared before the function name. The list must be mutable to allow swapping.Step 2: Verify implementation correctness
The function swaps elements at indicesiandjusing a temporary variable, which is correct.Final Answer:
fun <T> swap(list: MutableList<T>, i: Int, j: Int) { val temp = list[i]; list[i] = list[j]; list[j] = temp } -> Option CQuick Check:
Generic swap needs <T> before name and MutableList<T> [OK]
- Putting <T> after function name
- Using immutable List instead of MutableList
- Using MutableList<Any> losing type safety
Ready to prove it?
Pass the test and this pattern moves unknown
