Esc
  • Loading…
↑ ↓ to moveEnter to openEsc to close

Strings

Continue with the data structure you use every day but rarely look inside.

  • 19 Lessons
  • 19 Problems
  • 5 Patterns
  • 76 Illustrations
  • 38 Slides

Dry-run engine

Reverse a String In Place

Every character is just a byte sitting in a contiguous buffer.

O(1) Extra Memory
Input · String
s = "stressed"
Output · Returns
Press play to run it
How it works

While L < R, swap buf[L] and buf[R] in place, then step both pointers inward (L++, R--). Each cell shows its character and the byte it is stored as.

char_buffer[8] · ASCII

  • Left (L)0 → 's' (0x73)
  • Right (R)7 → 'd' (0x64)
  • Buffer"stressed"

Strings are immutable in JavaScript, Java and Python, so the bytes are first copied into a mutable char[] — one byte per ASCII character. L=0 holds 's' (0x73), R=7 holds 'd' (0x64). Since L < R, swap them in place, then move L → 1 and R → 6.

1 / 5

What you will learn

  • How characters become bytes, and why length and index can disagree
  • What immutability costs you, and when to reach for a builder instead
  • The two pointer pattern applied to reversal, palindromes, and partitioning
  • Sliding windows over character frequency maps
  • Anagram and grouping problems solved by canonical signatures
  • How substring search moves from O(N·M) brute force to O(N + M)

Why this course

A string looks like the easiest data structure in the language until a problem asks you to reverse it in place, and you discover str[i] = 'a' silently does nothing. Strings are arrays with extra rules — an encoding underneath, and in most languages immutability on top — and both rules change which solutions are even available to you.

This course starts at the byte level, shows you what your language actually allocates when you concatenate in a loop, then works through the five patterns that cover nearly every string question in an interview. Each pattern is introduced on a small example before you apply it to progressively harder problems.

Requirements

  • Comfort writing loops, conditionals, and functions in at least one language
  • The Arrays course, or equivalent familiarity with indexing and two pointers
  • No prior knowledge of character encodings — they are introduced from scratch

Course Contents