Int, Long, Float, Double number types in Kotlin - Time & Space Complexity
Let's see how the time it takes to work with different number types changes as we do more operations.
We want to know how the size of the input affects the time to process numbers like Int, Long, Float, and Double.
Analyze the time complexity of the following code snippet.
fun sumNumbers(numbers: List): Double {
var sum = 0.0
for (num in numbers) {
sum += num.toDouble()
}
return sum
}
This code adds up a list of numbers, converting each to Double before adding.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list once.
- How many times: Exactly once for each number in the input list.
As the list gets bigger, the time to add all numbers grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time grows directly with the number of items you add.
[X] Wrong: "Using Double or Float makes the code slower because they are more complex types than Int or Long."
[OK] Correct: The time to add numbers depends mostly on how many numbers you process, not the specific number type. The difference in speed between these types is usually very small and does not change the overall growth pattern.
Understanding how the size of data affects processing time is a key skill. It helps you write code that stays fast even as data grows.
"What if we changed the list to a nested list of numbers and summed all inner lists? How would the time complexity change?"
Practice
42?Solution
Step 1: Understand the number type categories
Int is used for normal whole numbers without decimals.Step 2: Match the number 42 to the type
42 is a whole number and fits in Int range.Final Answer:
Int -> Option CQuick Check:
Whole number 42 = Int [OK]
- Choosing Float or Double for whole numbers
- Using Long unnecessarily for small numbers
Solution
Step 1: Recall Kotlin Float literal syntax
Float literals require an 'f' or 'F' suffix to distinguish from Double.Step 2: Check each option
val pi: Float = 3.14f uses '3.14f' which is correct. val pi: Float = 3.14 misses the suffix. val pi: Float = 3.14Ff has invalid suffix 'Ff'. val pi: Float = 3.14d uses 'd' which is for Double.Final Answer:
val pi: Float = 3.14f -> Option DQuick Check:
Float literals end with 'f' [OK]
- Omitting 'f' suffix for Float literals
- Using 'd' suffix which is for Double
- Using invalid suffixes like 'Ff'
val a: Int = 100000
val b: Long = 10000000000L
val c: Float = 3.14f
val d: Double = 3.1415926535
println("$a $b $c $d")
Solution
Step 1: Understand variable types and precision
a is Int (100000), b is Long (10000000000L), c is Float (3.14f), d is Double (3.1415926535).Step 2: Check printed values and precision
Float prints as 3.1400001 due to precision limits, Double prints full precision (3.1415926535). So output matches 100000 10000000000 3.1400001 3.1415926535.Final Answer:
100000 10000000000 3.1400001 3.1415926535 -> Option AQuick Check:
Float prints with slight precision difference [OK]
- Thinking Float prints exactly 3.14
- Confusing Float and Double precision
- Misreading Long large number formatting
val bigNumber: Long = 12345678901234567890
val smallNumber: Int = 2147483648
Solution
Step 1: Check Long value range
Long can hold values up to 9,223,372,036,854,775,807. The bigNumber literal exceeds this, so it causes an error.Step 2: Check Int value range
Int max is 2,147,483,647. The smallNumber is 2,147,483,648 which is 1 more than max, so it causes an error.Step 3: Analyze the code
Both literals are too large for their types, causing compile errors.Final Answer:
Both numbers are too large for their types -> Option AQuick Check:
Int max 2147483647, Long max 9223372036854775807; both exceeded [OK]
- Ignoring suffix 'L' for Long literals
- Assuming Long can hold any big number
- Not checking Int max value
Options:
A) Long because it can store very large whole numbers
B) Int because it stores whole numbers
C) Float because it stores decimal numbers
D) Double because it stores decimal numbers with more precision
Solution
Step 1: Understand the population size and type limits
Population is about 1.4 billion (1,400,000,000). Int max is 2,147,483,647, so Int can hold it.Step 2: Consider best practice for large numbers
Though Int can hold 1.4 billion, Long is safer for very large whole numbers and future growth.Step 3: Decide on the best type
Long is best because it can store very large whole numbers beyond Int range.Final Answer:
Long because it can store very large whole numbers -> Option BQuick Check:
Long preferred for large integers like population [OK]
- Choosing Int without considering future growth
- Using Float or Double for whole numbers unnecessarily
- Ignoring future growth beyond Int range
Ready to prove it?
Pass the test and this pattern moves unknown
