Visualized DSA Strategies
Episode 6 Strings
▶ This video is coming to YouTube soon.
The complete guide to strings in JavaScript interviews. First how strings really work: UTF-16 code units, characters are numbers you read with charCodeAt, a string can never change, why += in a loop is O(n²) on paper and how V8's ropes often hide it, why an array of parts and join is the habit that's fast in every engine, what every common method costs, 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 JavaScript string is stored (UTF-16), and what s[i], slice, === and indexOf cost
- Characters are numbers: charCodeAt minus 97 for letter counts, minus 48 for digits, and String.fromCharCode back
- Immutability, why += in a loop is O(n²) on paper and when V8 hides it, and the array-of-parts toolbox: push, pop, join
- Counting letters with an array of 26, and two pointers from both ends
- Mapping letters both ways with two Maps, and an array used as a stack
- Adding huge numbers digit by digit, and counting words with split 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:24What's a string?
- 1:26Characters are numbers
- 2:31Character codes worth knowing
- 3:21Strings can't change
- 4:16Why += in a loop is O(n²), and V8's ropes
- 5:17An array: push in place
- 6:11The array-of-parts toolbox
- 7:17What string methods cost
- 7:53Five traps
- 8:53Nine string techniques
- 9:32Type 1 · Compress runs of letters
- 15:51Type 2 · First unique character
- 21:37Type 3 · Is it a palindrome?
- 27:24Type 4 · Same pattern?
- 33:16Type 5 · Remove adjacent duplicates
- 39:22Type 6 · Add two number strings
- 47:31Type 7 · Most common word
- 53:33Type 8 · Longest odd palindrome
- 1:00:00Type 9 · Find a word in a text
- 1:06:31Spot the technique
- 1:07:21Quiz: which technique?
- 1:08:08Recap
Quiz: test yourself
The 5 questions from the end of the episode. Pick an answer to see why it's right.