Explicit type declaration in Kotlin - Time & Space Complexity
Let's see how declaring types explicitly in Kotlin affects the time it takes for code to run.
We want to know if writing types by hand changes how fast the program works.
Analyze the time complexity of the following code snippet.
fun sumList(numbers: List): Int {
var sum: Int = 0 // explicit type declaration
for (num in numbers) {
sum += num
}
return sum
}
This function adds up all numbers in a list, using an explicit type for the sum variable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list.
- How many times: Once for every item in the list.
As the list gets bigger, the number of additions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets bigger.
[X] Wrong: "Writing the type explicitly makes the code slower because it adds extra work."
[OK] Correct: The explicit type is just for the programmer and compiler; it does not change how many steps the program takes when running.
Understanding how code runs helps you explain your choices clearly and confidently in interviews.
"What if we replaced the list with a set? How would the time complexity change?"
Practice
Solution
Step 1: Understand explicit type declaration
Explicit type declaration means telling Kotlin the exact type of a variable, like Int or String.Step 2: Compare with other options
The other options describe behaviors that are not related to explicit type declaration.Final Answer:
It tells Kotlin exactly what type a variable or value has. -> Option BQuick Check:
Explicit type declaration = telling exact type [OK]
- Thinking Kotlin guesses types without declaration
- Confusing explicit type with variable mutability
- Believing explicit type removes need for val/var
Solution
Step 1: Recall Kotlin variable declaration syntax
Kotlin usesvalorvarfollowed by variable name, colon, type, then assignment.Step 2: Check each option
var age: Int = 25 matches this syntax:var age: Int = 25. Others have syntax errors.Final Answer:
var age: Int = 25 -> Option AQuick Check:
Correct syntax uses colon before type [OK]
- Omitting colon before type
- Placing type after value
- Using wrong order of keywords
val number: Int = 10 var text: String = "Hello" text = "Hi" println(number) println(text)
Solution
Step 1: Analyze variable declarations and assignments
numberis aval(fixed) with value 10.textis avar(changeable) initially "Hello", then changed to "Hi".Step 2: Determine printed output
Printingnumberoutputs 10. Printingtextoutputs the updated value "Hi".Final Answer:
10 Hi -> Option CQuick Check:
val fixed, var changeable = 10 and Hi [OK]
- Thinking val can be reassigned
- Confusing initial and updated values
- Expecting syntax error for reassignment of var
val count: Int = 5 count = 10
Solution
Step 1: Understand val and var difference
valmeans the variable is read-only and cannot be reassigned after initialization.Step 2: Check the reassignment
The code tries to assign 10 tocountwhich is declared asval, causing an error.Final Answer:
Cannot reassign a value to val variable -> Option DQuick Check:
val is fixed, reassignment causes error [OK]
- Thinking val variables can be changed
- Confusing val and var keywords
- Ignoring reassignment rules
score that can change and must hold decimal numbers. Which explicit type declaration is correct in Kotlin?Solution
Step 1: Identify mutability requirement
The variablescoremust be changeable, so usevar, notval.Step 2: Choose correct type for decimal numbers
Decimal numbers requireDoubleorFloat, but value 95.5 is a Double by default.Step 3: Check options
var score: Double = 95.5 usesvarandDoublewith correct value. Others either useval, wrong type, or wrong value type.Final Answer:
var score: Double = 95.5 -> Option AQuick Check:
var + Double for changeable decimal [OK]
- Using val when variable should change
- Assigning decimal to Int type
- Putting string value for numeric type
Ready to prove it?
Pass the test and this pattern moves unknown
