Visualized DSA Strategies
Episode 6 Strings
▶ This video is coming to YouTube soon.
The complete guide to strings in Go interviews. First how Go strings really work: a string is a read-only row of bytes, characters are numbers (bytes and runes), why += in a loop is O(n²) and how a strings.Builder or a byte slice makes it O(n), what every common operation costs (a slice shares memory, []byte(s) copies), and five traps that catch everyone. Then nine techniques, each worked as a small problem in full: examples, the brute force animated beside the running line of code, the complete code, every line explained, and the better solution the same way.
What you'll learn
- How a Go string is stored, and what len, s[i], slicing, == and strings.Index cost
- Characters are numbers: bytes and runes, c − 'a' for letter counts, c − '0' for digits
- Immutability, why += in a loop is O(n²), and the strings.Builder and []byte toolbox
- Counting letters with a [26]int array, and two pointers from both ends
- Mapping letters both ways, and a byte slice used as a stack
- Adding huge numbers digit by digit, and counting words with strings.Fields and a map
- Expanding palindromes from their centre
- Finding a pattern with a rolling hash (the idea behind Rabin–Karp)
- How to spot which technique a problem wants
Chapters
- 0:00Intro
- 0:22What's a string?
- 1:23Characters are numbers
- 2:28Character codes worth knowing
- 3:09Strings can't change
- 3:59Why += in a loop is O(n²)
- 5:03strings.Builder: write in place
- 6:01The strings.Builder and []byte toolbox
- 7:35What string operations cost
- 8:20Five traps
- 9:20Nine string techniques
- 10:00Type 1 · Compress runs of letters
- 16:28Type 2 · First unique character
- 22:20Type 3 · Is it a palindrome?
- 28:22Type 4 · Same pattern?
- 34:19Type 5 · Remove adjacent duplicates
- 40:51Type 6 · Add two number strings
- 48:53Type 7 · Most common word
- 54:59Type 8 · Longest odd palindrome
- 1:01:40Type 9 · Find a word in a text
- 1:08:05Spot the technique
- 1:08:56Quiz: which technique?
- 1:09:44Recap
Quiz: test yourself
The 5 questions from the end of the episode. Pick an answer to see why it's right.