Comparison operators in Kotlin - Time & Space Complexity
We want to understand how fast comparison operators run in Kotlin.
How does the time to compare values change as the input size grows?
Analyze the time complexity of the following code snippet.
fun isGreater(a: Int, b: Int): Boolean {
return a > b
}
fun compareArrays(arr1: IntArray, arr2: IntArray): Boolean {
for (i in arr1.indices) {
if (arr1[i] != arr2[i]) return false
}
return true
}
This code compares two integers and also compares two arrays element by element.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing two integers or array elements.
- How many times: For arrays, the comparison happens once per element, so as many times as the array length.
Comparing two single numbers takes the same time no matter what.
Comparing two arrays takes longer as the arrays get bigger, because each element is checked.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the size of the arrays.
Time Complexity: O(n)
This means the time to compare two arrays grows in a straight line with the number of elements.
[X] Wrong: "Comparing two arrays always takes the same time regardless of size."
[OK] Correct: Actually, the program checks each element one by one, so bigger arrays take more time.
Understanding how comparison scales helps you explain efficiency clearly and shows you know what happens behind simple operations.
"What if we compare arrays but stop after finding the first difference? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of comparison operators
In Kotlin, the operator==is used to check if two values are equal.Step 2: Identify the correct operator for equality
The operator==returns true if both values are equal, otherwise false.Final Answer:
== -> Option DQuick Check:
Equality operator = == [OK]
- Confusing '==' with '=' which is assignment
- Using '!=' to check equality
- Mixing up '>' and '<=' operators
a is not equal to variable b?Solution
Step 1: Recall Kotlin syntax for inequality
Kotlin uses!=to check if two values are not equal.Step 2: Verify each option
a != bis correct;a <> bis invalid in Kotlin;a =! bis incorrect syntax;a !== bchecks referential inequality, not value inequality.Final Answer:
a != b -> Option AQuick Check:
Not equal operator = != [OK]
- Using '<>' which is not valid in Kotlin
- Swapping '=' and '!' in 'a =! b'
- Confusing '!=' with '!==', which checks references
val x = 10 val y = 20 println(x > y)
Solution
Step 1: Evaluate the comparison expression
The expressionx > ycompares if 10 is greater than 20, which is false.Step 2: Understand the println output
Printing the boolean result of the comparison will outputfalse.Final Answer:
false -> Option AQuick Check:
10 > 20 = false [OK]
- Assuming 10 > 20 is true
- Confusing output with variable values
- Expecting a number instead of boolean output
val a = 5
val b = 10
if (a = b) {
println("Equal")
} else {
println("Not Equal")
}Solution
Step 1: Check the if condition syntax
The condition usesa = b, which is assignment, not comparison.Step 2: Correct the operator for comparison
Use==to compare values, so it should bea == b.Final Answer:
Using '=' instead of '==' in if condition -> Option CQuick Check:
Comparison needs '==' not '=' [OK]
- Using '=' in if condition instead of '=='
- Thinking semicolons are required in Kotlin
- Assuming val variables must be mutable for comparison
val a = 7 val b = 7 val c = 10 println((a == b) && (b < c))
Solution
Step 1: Evaluate the first comparison (a == b)
Since bothaandbare 7,a == bis true.Step 2: Evaluate the second comparison (b < c)
Since 7 is less than 10,b < cis true.Step 3: Evaluate the logical AND
Both conditions are true, sotrue && trueis true.Final Answer:
true -> Option BQuick Check:
(7 == 7) && (7 < 10) = true [OK]
- Confusing && with || operator
- Misreading comparison results
- Expecting numeric output instead of boolean
Ready to prove it?
Pass the test and this pattern moves unknown
