🚀 Phases 1–5 are live — Days 1–17 cover the foundations and the algorithmic patterns. See the roadmap →
Day 2 - Arrays and StringsStringsIntroduction

Introduction to Strings

Strings are sequences of characters — the building blocks of text processing, user input, file handling, and nearly everything you see on a screen. If arrays are the bread of data structures, strings are the butter.

What is a String?

A string is an ordered sequence of characters. Depending on the language, a string might be:

  • An array of characters (C/C++) — literally a char[] with a null terminator \0
  • An immutable object (Python, Java) — once created, its contents cannot be changed
  • A dynamic primitive (JavaScript) — primitive type but with object-like methods
string greeting = "HELLO"
H
[0]
E
[1]
L
[2]
L
[3]
O
[4]
\0
[5]

GIF

In C, strings are null-terminated character arrays. The \0 at the end tells functions like printf where the string ends. Forgetting it causes bugs that read garbage memory — a classic source of security vulnerabilities.

Strings vs. Character Arrays

This distinction trips up many beginners:

FeatureCharacter Array (C)String Object (Python/Java)
StorageContiguous chars + \0Internal char array + metadata
MutabilityMutable (can modify in-place)Immutable (creates new string)
SizeFixed at declarationDynamic (grows as needed)
Null terminatorRequired (\0)Handled internally

GIF

⚠️

Immutability trap: In Python and Java, every time you “modify” a string, you actually create a brand new string. Doing s += "x" in a loop is O(n^2) total work because each concatenation copies the entire string. Use "".join(list) in Python or StringBuilder in Java instead.

How Strings Are Stored in Memory

Strings are stored as contiguous blocks of characters, just like arrays. Each character occupies a fixed number of bytes based on the encoding:

EncodingBytes per CharCharacter Set
ASCII1 byte128 characters (English, digits, symbols)
Extended ASCII1 byte256 characters
UTF-81-4 bytesAll Unicode (1.1M+ characters)
UTF-162-4 bytesUsed internally by Java, JavaScript
ASCII values: H=72, e=101, l=108, l=108, o=111
H
[0]
e
[1]
l
[2]
l
[3]
o
[4]

Why encoding matters for interviews: When a problem says “lowercase English letters only,” you know there are exactly 26 possible characters — this lets you use a fixed-size array of 26 instead of a hash map. That constraint is a hint.

Types of String Problems

String problems in interviews generally fall into these categories:

CategoryTechniquesExample Problems
TraversalSingle/double pointerReverse a string, palindrome check
MatchingHash map, KMP, Rabin-KarpPattern matching, anagram detection
TransformationStack, recursionString to integer, decode ways
SubstringSliding window, DPLongest substring without repeating
ComparisonSorting, character countingAnagram groups, common prefix

Try It Yourself

Type any string and explore how it looks as a character array. Try checking palindromes, reversing, and counting characters:

Interactive String Explorer
h
[0]
e
[1]
l
[2]
l
[3]
o
[4]
Length: 5

Strings vs. Arrays — Key Differences

If you just came from the Arrays section, here is what changes with strings:

  1. Character encoding — arrays hold numbers, strings hold encoded characters
  2. Immutability — most languages make strings immutable (arrays usually aren’t)
  3. Built-in methods — strings come with rich libraries (split, join, replace, regex)
  4. Null termination — C strings need \0, arrays don’t
  5. Comparison semantics — comparing strings requires character-by-character checks, while arrays of primitives compare values
String A
l
i
s
t
e
n
String B
s
i
l
e
n
t

The two strings above share only some characters at the same positions, but they are anagrams of each other (same characters, different order). Spotting relationships like this is the core of string problem-solving.

Quick Check

In Python, what happens when you do s = 'hello'; s[0] = 'H'?
Why is concatenating strings in a loop (s += char) considered O(n^2) in languages with immutable strings?