String Basics and Memory Representation in DSA Go - Time & Space Complexity
We want to understand how the time to work with strings grows as the string gets longer.
How does the computer handle each character and how long does it take?
Analyze the time complexity of the following code snippet.
package main
import "fmt"
func printChars(s string) {
for i := 0; i < len(s); i++ {
fmt.Printf("%c ", s[i])
}
fmt.Println()
}
func main() {
printChars("hello")
}
This code prints each character of a string one by one by accessing each character in order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the string.
- How many times: Exactly once for each character in the string (length n).
As the string gets longer, the number of steps grows directly with the number of characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 character prints |
| 100 | 100 character prints |
| 1000 | 1000 character prints |
Pattern observation: The work grows in a straight line with the string length.
Time Complexity: O(n)
This means the time to process the string grows directly with its length.
[X] Wrong: "Accessing a character in a string is instant and does not depend on string length."
[OK] Correct: While accessing a character by index is fast, looping through all characters means the total time depends on how many characters there are.
Understanding how string operations scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we used recursion to print each character instead of a loop? How would the time complexity change?"
Practice
var s string = "hello"Solution
Step 1: Understand Go string type
In Go, a string is a sequence of bytes that represent characters, usually UTF-8 encoded.Step 2: Check mutability and type
Strings in Go are immutable, so they cannot be changed after creation. They are not arrays or pointers to integers.Final Answer:
A sequence of bytes representing characters -> Option AQuick Check:
Go strings = immutable byte sequences [OK]
- Thinking strings are mutable arrays
- Confusing strings with pointers
- Assuming strings store integers
Solution
Step 1: Check string literal syntax in Go
Go uses double quotes "" for string literals, single quotes '' are for rune (character) literals.Step 2: Validate declaration syntax
Correct declaration uses var keyword, type string, and double quotes for value.Final Answer:
var s string = "hello" -> Option BQuick Check:
Use double quotes for strings in Go [OK]
- Using single quotes for strings
- Omitting quotes around string value
- Using incorrect variable declaration syntax
package main
import "fmt"
func main() {
s := "GoLang"
fmt.Println(len(s))
}Solution
Step 1: Understand the string value
The string "GoLang" has 6 characters: G, o, L, a, n, g.Step 2: Use len function on string
len(s) returns the number of bytes in the string. Since all characters are ASCII, length is 6.Final Answer:
6 -> Option AQuick Check:
len("GoLang") = 6 [OK]
- Counting characters incorrectly
- Expecting len() to count runes instead of bytes
- Assuming error due to missing imports
package main
import "fmt"
func main() {
s := "hello"
s[0] = 'H'
fmt.Println(s)
}Solution
Step 1: Check string mutability in Go
Strings in Go are immutable, so you cannot change individual bytes like s[0].Step 2: Analyze the assignment statement
Attempting s[0] = 'H' causes a compile-time error because strings cannot be modified.Final Answer:
Strings are immutable; cannot assign to s[0] -> Option DQuick Check:
Strings immutable = no s[0] assignment [OK]
- Trying to modify string bytes directly
- Ignoring immutability errors
- Confusing strings with byte slices
s := "GoπLang", what will len(s) return and why?Solution
Step 1: Analyze string content and encoding
The string "GoπLang" has 7 characters: G, o, π, L, a, n, g. The emoji π is a Unicode character encoded in UTF-8 using 4 bytes.Step 2: Calculate total byte length
Characters G, o, L, a, n, g are 1 byte each (6 bytes). The emoji is 4 bytes. Total bytes = 6 + 4 = 10 bytes.Step 3: Verify len(s) output
len(s) returns the number of bytes, so it returns 10, not the number of characters.Final Answer:
10, because emoji is 4 bytes plus other characters -> Option CQuick Check:
len counts bytes, emoji uses multiple bytes [OK]
- Assuming len counts characters, not bytes
- Ignoring multi-byte encoding of emojis
- Thinking emoji counts as one byte
Ready to prove it?
Pass the test and this pattern moves unknown
