Constant values with const val in Kotlin - Time & Space Complexity
Let's see how using constant values affects the time it takes for a program to run.
We want to know how the program's work changes when it uses fixed values.
Analyze the time complexity of the following code snippet.
const val MAX_COUNT = 100
fun printMessage() {
for (i in 1..MAX_COUNT) {
println("Hello, Kotlin!")
}
}
This code prints a message a fixed number of times using a constant value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints the message.
- How many times: Exactly 100 times, because MAX_COUNT is a constant.
Since the loop runs a fixed 100 times, the work stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 (fixed) |
| 100 | 100 (fixed) |
| 1000 | 100 (fixed) |
Pattern observation: The number of operations does not grow with input size because the count is constant.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is.
[X] Wrong: "Since there is a loop, the time must grow with input size."
[OK] Correct: Here, the loop count is fixed by a constant, so the time does not change with input size.
Understanding when code runs in constant time helps you explain efficiency clearly and confidently.
"What if MAX_COUNT was a variable input instead of a constant? How would the time complexity change?"
Practice
const val in Kotlin?Solution
Step 1: Understand what
const valmeansconst valis used to declare constants in Kotlin, which are values fixed at compile time and cannot be changed later.Step 2: Compare options with the definition
To declare a constant value that cannot change during program execution correctly states thatconst valdeclares a constant value that never changes. Other options describe variables or functions, which are not correct.Final Answer:
To declare a constant value that cannot change during program execution -> Option AQuick Check:
const val = fixed value [OK]
- Thinking const val can be changed like a variable
- Confusing const val with regular val or var
- Assuming const val can hold complex objects
MAX_COUNT with value 100 in Kotlin?Solution
Step 1: Recall the correct syntax for constants
In Kotlin, constants are declared usingconst valfollowed by the name and value.Step 2: Check each option
const val MAX_COUNT = 100 usesconst val MAX_COUNT = 100, which is the correct syntax. val MAX_COUNT = 100 missesconst, C missesval, and B usesvarwhich is mutable.Final Answer:
const val MAX_COUNT = 100 -> Option DQuick Check:
const val + name + = + value = correct syntax [OK]
- Omitting const keyword
- Using var instead of val
- Writing const without val
const val PI = 3.14
fun main() {
println("Value of PI: $PI")
}Solution
Step 1: Understand string interpolation in Kotlin
Theprintlnstatement uses$PIwhich means it will replace$PIwith the value of the constantPI.Step 2: Check the constant value and output
PIis declared as 3.14, so the output will be "Value of PI: 3.14" exactly.Final Answer:
Value of PI: 3.14 -> Option CQuick Check:
String interpolation replaces $PI with 3.14 [OK]
- Thinking $PI prints literally as $PI
- Expecting integer output instead of float
- Assuming compilation error due to const val usage
const val greeting = "Hello"
fun main() {
greeting = "Hi"
println(greeting)
}Solution
Step 1: Understand const val immutability
const valdeclares a constant that cannot be changed after initialization.Step 2: Analyze the reassignment
The code tries to assign a new value togreetingwhich is a constant. This causes a compilation error.Final Answer:
Cannot reassign a value to a const val -> Option BQuick Check:
const val = fixed, no reassignment allowed [OK]
- Trying to assign new value to const val
- Thinking semicolon is required in Kotlin
- Declaring const val inside functions (allowed only at top-level or object)
APP_NAME that can be used throughout your Kotlin app. Which of the following is the best way to declare it to ensure it is a compile-time constant accessible everywhere?Solution
Step 1: Understand where const val can be declared
In Kotlin,const valmust be declared at the top level or inside an object to be a compile-time constant accessible everywhere.Step 2: Evaluate each option
Declareconst val APP_NAME = "MyApp"at the top level or inside an object correctly declaresconst val APP_NAME = "MyApp"at the top level or inside an object. Declareval APP_NAME = "MyApp"inside main() function usesvalinside a function, which is not compile-time constant. Declarevar APP_NAME = "MyApp"inside a class constructor usesvarwhich is mutable. Declareconst var APP_NAME = "MyApp"inside a class uses invalid syntaxconst var.Final Answer:
Declare const val APP_NAME = "MyApp" at the top level or inside an object -> Option AQuick Check:
const val + top-level/object = global constant [OK]
- Declaring const val inside functions
- Using var with const keyword
- Using const without val
Ready to prove it?
Pass the test and this pattern moves unknown
