A string is a sequence of characters — letters, digits, spaces, punctuation, emoji. "hello" is five characters in a row. Like an array, each character has an index, counted from 0, and the number of characters is the string's length.
Almost everything a program reads or shows is text: names, passwords, messages, URLs, file paths, JSON from an API, source code, even DNA ("GATTACA"). Searching, validating, formatting and comparing text are some of the most common jobs in real software — so they are some of the most common interview questions too.
Interviewers like string questions for another reason: they look easy and hide real decisions. Can you change the string, or must you build a new one? Is the alphabet just 26 lowercase letters, or any character? Does += in a loop quietly cost O(n²)? Getting those details right is what the introduction is for.
Everything you know about arrays carries over: characters sit next to each other in memory, s[i] is O(1), and a loop visits them in order. Three things are different, and each one changes how you solve problems:
'a' is stored as 97, and an encoding decides how each character becomes bytes. That's why c - 'a' turns a letter into a position 0–25. Next lesson.Here are the moves every string solution uses — reading the length, reading a character by index, and looping — in one small function that counts the vowels in a string.
function countVowels(s) { let count = 0; for (let i = 0; i < s.length; i++) { // s.length: how many characters const ch = s[i].toLowerCase(); // s[i]: the character at index i if ("aeiou".includes(ch)) count++; } return count;}One pass over n characters: O(n) time, O(1) space. Notice the small differences: Java reads characters with charAt(i) and calls length() as a method; Python loops over characters directly; C++ takes the string by const string& so it isn't copied.
length means different things in different languages.s += c in a loop can be O(n²), and how builders make it O(n).Was this helpful?