Print and println output in Kotlin - Time & Space Complexity
Let's explore how the time taken by print and println commands changes as we print more items.
We want to know how the program's running time grows when printing more lines.
Analyze the time complexity of the following code snippet.
fun printNumbers(n: Int) {
for (i in 1..n) {
println(i)
}
}
This code prints numbers from 1 up to n, each on its own line.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs and calls println once per number.
- How many times: Exactly n times, where n is the input size.
As n grows, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The work grows directly in step with the number of items printed.
Time Complexity: O(n)
This means the time to print grows in a straight line as you print more lines.
[X] Wrong: "Printing multiple lines happens instantly or in constant time regardless of how many lines."
[OK] Correct: Each print takes time, so more lines mean more total time, growing with the number of lines.
Understanding how simple loops like printing scale helps you explain program speed clearly and confidently.
"What if we used print instead of println inside the loop? How would the time complexity change?"
Practice
print and println in Kotlin?Solution
Step 1: Understand
printbehaviorprintoutputs text exactly as given and stays on the same line.Step 2: Understand
printlnbehaviorprintlnoutputs text and then moves the cursor to the next line.Final Answer:
printoutputs text without a newline;printlnadds a newline after output. -> Option CQuick Check:
print no newline, println newline = A [OK]
- Confusing which function adds a newline
- Thinking print adds a newline
- Assuming println outputs only numbers
Solution
Step 1: Check syntax correctness
Onlyprintln("Hello")uses correct function call syntax with parentheses and quotes.Step 2: Verify output behavior
println("Hello")prints "Hello" and moves to a new line automatically.Final Answer:
println("Hello") -> Option AQuick Check:
Correct syntax with println = A [OK]
- Missing parentheses around arguments
- Using square brackets instead of parentheses
- Omitting quotes around strings
print("Hi")
println(" there")
print("Welcome")Solution
Step 1: Trace first print statement
print("Hi")outputs "Hi" without newline.Step 2: Trace println statement
println(" there")outputs " there" and moves to next line.Step 3: Trace last print statement
print("Welcome")outputs "Welcome" on the new line.Final Answer:
Hi there Welcome -> Option DQuick Check:
print no newline, println newline = D [OK]
- Assuming print adds newline
- Ignoring space in " there"
- Thinking all output is on one line
print("Hello")
println "World"Solution
Step 1: Check syntax of print statement
print("Hello")is correct with parentheses and quotes.Step 2: Check syntax of println statement
println "World"is missing parentheses around the argument, which is required in Kotlin.Final Answer:
Missing parentheses in println call -> Option AQuick Check:
Function calls need parentheses = B [OK]
- Omitting parentheses in function calls
- Thinking print needs println to work
- Confusing quotes usage
Solution
Step 1: Print numbers on same line with spaces
Usingprint("1 2 3")outputs "1 2 3" on the same line.Step 2: Print "Done" on next line
println("Done")prints "Done" and moves to the next line.Final Answer:
print("1 2 3") println("Done") -> Option BQuick Check:
Use print for same line, println for new line = A [OK]
- Using println for each number causing new lines
- Using print after println without newline
- Adding \n inside print incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
