Basic input concepts in Go - Time & Space Complexity
When we read input in a program, the time it takes depends on how much data we get.
We want to see how the program's work grows as the input size grows.
Analyze the time complexity of the following code snippet.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
fmt.Println(line)
}
This code reads one line of input from the user and prints it back.
- Primary operation: Reading characters one by one until a newline is found.
- How many times: As many characters as the input line length.
As the input line gets longer, the program reads more characters, so it takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character reads |
| 100 | About 100 character reads |
| 1000 | About 1000 character reads |
Pattern observation: The work grows directly with the input size.
Time Complexity: O(n)
This means the time to read input grows in a straight line with how many characters you type.
[X] Wrong: "Reading input always takes the same time no matter how much data there is."
[OK] Correct: The program reads each character one by one, so more characters mean more time.
Understanding how input size affects reading time helps you explain program speed clearly and shows you think about efficiency.
"What if we read multiple lines instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand input function role
Input functions allow the program to receive data from the user while it runs.Step 2: Differentiate input from output
Printing messages is output, not input. Declaring variables or creating functions are unrelated.Final Answer:
To get information from the user during program execution -> Option AQuick Check:
Input = get user data [OK]
- Confusing input with output (printing)
- Thinking input declares variables
- Mixing input with function creation
Solution
Step 1: Identify input reading functions
fmt.Scanln reads a single word or token from user input into a variable.Step 2: Eliminate other options
fmt.Println prints output, bufio.NewReader reads full lines, and declaring a variable alone doesn't read input.Final Answer:
fmt.Scanln(&variable) -> Option BQuick Check:
Single word input uses fmt.Scanln [OK]
- Using fmt.Println to read input
- Confusing bufio.NewReader with Scanln for single words
- Forgetting to pass variable address with &
var name string
fmt.Print("Enter name: ")
fmt.Scanln(&name)
fmt.Println("Hello,", name)
If the user types: John and presses Enter.Solution
Step 1: Understand input reading
fmt.Scanln reads the word 'John' into variable name.Step 2: Understand output printing
fmt.Println prints "Hello," followed by the value of name, which is "John".Final Answer:
Hello, John -> Option DQuick Check:
Input 'John' prints 'Hello, John' [OK]
- Expecting input prompt to print again
- Thinking Scanln reads empty string
- Confusing output with input prompt
var input string
reader := bufio.NewReader(os.Stdin)
input, _ = reader.ReadString('\n')
fmt.Println(input)Solution
Step 1: Check package imports
bufio and os packages must be imported to use bufio.NewReader and os.Stdin.Step 2: Verify variable declaration
input is declared once and assigned, which is correct. ReadString reads until newline correctly. fmt.Println can print strings.Final Answer:
It does not import the bufio and os packages -> Option AQuick Check:
Missing imports cause errors [OK]
- Forgetting to import required packages
- Redeclaring variables with := after declaration
- Assuming ReadString reads partial input
Solution
Step 1: Identify method for full line input
bufio.NewReader with ReadString('\n') reads the entire line including spaces.Step 2: Remove trailing newline
strings.TrimSpace removes extra spaces and newline characters from input.Final Answer:
Use bufio.NewReader(os.Stdin).ReadString('\n') and strings.TrimSpace() -> Option CQuick Check:
Full line input + trim newline = Use bufio.NewReader(os.Stdin).ReadString('\n') and strings.TrimSpace() [OK]
- Using Scanln which stops at space
- Using Scanf with %s reads only one word
- Trying to read input with Println
Ready to prove it?
Pass the test and this pattern moves unknown
