Range operator (..) and in operator in Kotlin - Time & Space Complexity
We want to understand how the time it takes to check if a number is within a range changes as the range size grows.
How does using the range operator and the in operator affect the work done by the program?
Analyze the time complexity of the following code snippet.
val n = 100 // example value for n
val range = 1..n
val number = 50
if (number in range) {
println("Number is in the range")
} else {
println("Number is not in the range")
}
This code creates a range from 1 to n and checks if a fixed number is inside that range.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking if the number is within the range using the in operator.
- How many times: This check happens once per program run here.
Since the range is defined by start and end numbers, checking if a number is inside is a simple comparison.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 comparison |
| 100 | 1 comparison |
| 1000 | 1 comparison |
Pattern observation: The number of operations stays the same no matter how big the range is.
Time Complexity: O(1)
This means checking if a number is in a range takes the same amount of time regardless of the range size.
[X] Wrong: "Checking if a number is in a large range takes longer because the range is bigger."
[OK] Correct: The in operator with ranges uses simple comparisons, not loops, so size does not affect time.
Understanding how simple operations like range checks work helps you explain efficiency clearly and confidently in interviews.
"What if we checked if a number is in a list instead of a range? How would the time complexity change?"
Practice
1..5 represent?Solution
Step 1: Understand the range operator
The..operator creates a range including both start and end values.Step 2: Interpret
This means all numbers from 1 up to 5, including 1 and 5.1..5Final Answer:
A range of numbers from 1 to 5 inclusive -> Option DQuick Check:
1..5= numbers 1 to 5 [OK]
- Thinking it creates a list instead of a range
- Assuming it excludes the last number
- Confusing it with a function call
x is between 10 and 20 inclusive in Kotlin?Solution
Step 1: Recall Kotlin's in operator syntax
Kotlin usesinto check if a value is inside a range.Step 2: Match syntax with options
Onlyif (x in 10..20)correctly usesinand the range operator...Final Answer:
if (x in 10..20) { } -> Option BQuick Check:
Useinwith..for range checks [OK]
- Using 'between' or 'inside' which are not Kotlin keywords
- Trying chained comparisons like 10 <= x <= 20
- Missing the range operator '..'
val x = 7
if (x in 1..5) println("Inside") else println("Outside")Solution
Step 1: Understand the range check
The range1..5includes numbers 1, 2, 3, 4, 5.Step 2: Check if x=7 is in the range
7 is not between 1 and 5, so the condition is false.Final Answer:
Outside -> Option AQuick Check:
7 not in 1..5 means print "Outside" [OK]
- Assuming 7 is inside 1..5
- Confusing inclusive ranges with exclusive
- Expecting a syntax error
for (i in 1..5)
if (i in 3..1) println(i)Solution
Step 1: Analyze the range 3..1
In Kotlin, a range with start greater than end (3..1) creates an empty range, which is valid.Step 2: Verify code execution
The for loop runs for i=1 to 5, buti in 3..1is always false for any i, so nothing prints. No syntax or runtime errors occur.Final Answer:
No error, code runs fine -> Option CQuick Check:
Empty ranges (start > end) are valid in Kotlin [OK]
- Thinking reversed ranges like 3..1 are invalid
- Assuming the for loop syntax is wrong
- Expecting a syntax error or crash with empty range
Solution
Step 1: Understand the range and step usage
2..10 step 2creates numbers 2, 4, 6, 8, 10.Step 2: Check each option
for (i in 2..10 step 2) println(i) prints 2,4,6,8,10 directly. for (i in 2..10) if (i % 2 != 0) println(i) checksi % 2 != 0and prints odds 3,5,7,9. for (i in 2 until 10 step 2) println(i) usesuntilexcluding 10 (2,4,6,8). for (i in 2..10 step 3) println(i) steps by 3 (2,5,8).Final Answer:
for (i in 2..10 step 2) println(i) -> Option AQuick Check:
Use step 2 with inclusive range for even numbers [OK]
- Using wrong modulo condition like % 2 != 0
- Using 'until' which excludes the end value
- Using wrong step size like 3
Ready to prove it?
Pass the test and this pattern moves unknown
