Kotlin REPL and script mode - Time & Space Complexity
We want to understand how the time it takes to run Kotlin code changes when using REPL or script mode.
How does the way we run code affect how long it takes as the code grows?
Analyze the time complexity of the following Kotlin script that sums numbers.
fun main() {
val n = 10 // Assume n is a positive integer input
var sum = 0
for (i in 1..n) {
sum += i
}
println(sum)
}
This script adds numbers from 1 to n and prints the total.
Look for loops or repeated steps.
- Primary operation: The for-loop adding each number to sum.
- How many times: The loop runs n times, once for each number.
As n gets bigger, the loop runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of steps grows directly with n.
Time Complexity: O(n)
This means the time to run the script grows in a straight line as the input size increases.
[X] Wrong: "Running code in REPL or script mode makes it run instantly no matter the input size."
[OK] Correct: REPL and script mode run code line by line, but the time still depends on how many steps the code has, like loops running many times.
Understanding how code runs in different modes helps you explain performance clearly and shows you know how input size affects running time.
"What if we replaced the for-loop with a built-in sum function? How would the time complexity change?"
Practice
Solution
Step 1: Understand REPL meaning
Kotlin REPL stands for Read-Eval-Print Loop, which runs code interactively.Step 2: Identify REPL use
It lets you type and run Kotlin code line by line without compiling a full program.Final Answer:
To run Kotlin code interactively line by line -> Option AQuick Check:
Kotlin REPL = Interactive code running [OK]
- Confusing REPL with full project compilation
- Thinking REPL is for app deployment
- Assuming REPL manages dependencies
Solution
Step 1: Recall Kotlin file types
Kotlin source files usually use .kt extension for normal programs.Step 2: Identify script file extension
Kotlin scripts use .kts extension to run code without full compilation.Final Answer:
.kts -> Option AQuick Check:
Kotlin script files = .kts [OK]
- Choosing .kt which is for normal Kotlin files
- Assuming .kotlin or .skr are valid extensions
- Mixing script files with compiled files
val x = 5 val y = 10 x + y
Solution
Step 1: Evaluate variable assignments
x is assigned 5, y is assigned 10 in REPL environment.Step 2: Calculate expression result
x + y equals 5 + 10 which is 15.Final Answer:
15 -> Option DQuick Check:
5 + 10 = 15 [OK]
- Concatenating numbers as strings (510)
- Printing expression text instead of result
- Assuming variables are undefined in REPL
example.kts:
val name = "Alice" println(Name)
Solution
Step 1: Check variable usage
Variable declared as 'name' but used as 'Name' which differs in case.Step 2: Understand Kotlin case sensitivity
Kotlin variables are case sensitive, so 'Name' is undefined causing error.Final Answer:
Variable 'Name' is not defined (case sensitive) -> Option BQuick Check:
Variable names are case sensitive [OK]
- Thinking semicolons are mandatory
- Believing println is disallowed in scripts
- Using single quotes for strings in Kotlin
Solution
Step 1: Compare quick testing methods
Creating a full project is slow; writing Java adds complexity.Step 2: Choose between script and REPL
REPL lets you define and test functions interactively without saving files.Step 3: Understand REPL advantage
REPL is fastest for quick tests; scripts require saving and running files.Final Answer:
Use Kotlin REPL to define and test the function interactively -> Option CQuick Check:
REPL = fastest interactive testing [OK]
- Assuming scripts are faster than REPL
- Thinking full projects are needed for tests
- Confusing Java code with Kotlin quick tests
Ready to prove it?
Pass the test and this pattern moves unknown
