Basic Operations
Two candidates count the letters in a string. One reaches for a hash map; the other uses a plain 26-slot array and finishes faster with less memory. Same answer — but one knows a trick that the alphabet hands you for free. Most string “performance” in interviews comes down to knowing a few facts like this cold.
Every string operation you’ll encounter in interviews boils down to these fundamentals. Knowing their time complexities is the difference between an O(n) solution and an accidental O(n^2) one.
Common Patterns to Remember
The 26-array trick: When a problem involves only lowercase English letters,
use int freq[26] (or [0]*26 in Python) indexed by char - 'a'. This is faster
and more space-efficient than a hash map for character counting.
# Character frequency with the 26-array trick
def char_freq(s):
freq = [0] * 26
for ch in s.lower():
if ch.isalpha():
freq[ord(ch) - ord('a')] += 1
return freqRecall the index math
The whole trick is one line: turning a character into a slot 0–25. Fill it in from memory:
Quick Check
These primitives (and the 26-array trick) are the building blocks for string algorithms — pattern matching, anagrams, and palindromes — which is where they combine into real interview solutions.