Almost every string question you will see in a coding interview is a variation on one of five patterns: two pointers, the sliding window, frequency counting, stack-based parsing, and custom ordering. If you can recognize which pattern a prompt maps to in the first minute, you have already done the hard part. This article breaks down each pattern, points to concrete problems from our question bank to drill, and covers the language internals (string immutability) that interviewers love to probe once your solution works.
We run mock interviews all day, and the single biggest time sink we see is candidates treating each string problem as brand new. They are not. Learn the pattern taxonomy and the individual problems collapse into a handful of templates.
The five patterns that cover most string questions
Here is the map. Memorize the trigger phrases, because the prompt almost always tells you which tool to reach for.
| Pattern | Trigger phrase in the prompt | Typical complexity |
|---|---|---|
| Two pointers | "reverse", "palindrome", "in place", "swap" | O(n) time, O(1) extra |
| Sliding window | "longest/shortest substring", "at most k" | O(n) time |
| Frequency counting | "anagram", "appears both", "count of each" | O(n) time, O(k) space |
| Stack parsing | "parentheses", "path", "nested", "operations" | O(n) time |
| Custom ordering | "sorted according to", "alien", "comparator" | O(n) or O(n log n) |
The rest of this article works through each one with problems you can practice right now.
Two pointers: reversing and in-place edits
Two pointers is the first pattern to master because it is the cheapest: O(n) time and usually O(1) extra space. You place one index at each end (or a fast and slow pointer) and move them toward each other.
The clean drill for this is Reverse Vowels of a String. You walk a left pointer forward until it hits a vowel and a right pointer backward until it hits a vowel, swap, and repeat. The trap most candidates fall into is rebuilding the string on every swap instead of converting to a mutable structure once, doing all swaps, then joining at the end. In an immutable-string language that mistake turns an O(n) solution into O(n squared).
DI String Match is a sneakier member of the family. You are given a string of I and D characters and must reconstruct a permutation of [0, n]. The elegant answer uses two pointers over the value range: assign the current low value on I and the current high value on D. It is a good example of two pointers operating over a numeric range rather than over the string indices themselves, which is exactly the kind of reframing interviewers want to see you make out loud.
Sliding window and frequency counting
These two patterns travel together. A sliding window maintains a contiguous range while a frequency map (usually a fixed-size array of 26 for lowercase letters, or a hash map) tracks what is inside it. The window expands on the right until a condition breaks, then contracts on the left. As one interview reference on the pattern puts it, the window expands until a condition is met, then contracts to find the optimal size, which keeps the whole scan at O(n).
Longest Nice Substring is a great practice problem here, and it is subtle. A substring is "nice" if every letter that appears shows up in both uppercase and lowercase. The pure sliding window is awkward because the validity condition is not monotonic, so the intended solution is often a divide-and-conquer split on the first "bad" character. We like this problem in mocks precisely because candidates who reflexively reach for a window get stuck, and the interesting signal is whether they notice the condition does not behave the way a standard window needs it to.
When you do have a monotonic condition ("at most k distinct characters", "no repeats"), the window template is mechanical: grow right, update counts, shrink left while the invariant is violated, record the best answer. Practice writing that template until it is muscle memory, because the same skeleton solves a dozen named problems.
Stack-based parsing: parentheses, paths, and operations
The moment a prompt involves matching, nesting, or sequential operations that can cancel each other, think stack.
- Crawler Log Folder is the gentlest entry point. You process folder operations (
"../"to go up,"./"to stay, a name to go down) and report the final depth. A counter or a stack both work, and the counter version is a nice chance to argue why you do not always need the full stack. - Maximum Nesting Depth of Two Valid Parentheses Strings asks you to split a valid parentheses string into two subsequences that minimize the maximum nesting depth. The trick is that you do not need a literal stack at all: assign parentheses to group A or B based on the parity of the current depth. Recognizing that depth is all you need to track is the insight being tested.
- Process String with Special Operations I is a build-as-you-go problem where characters like
*,#, and%transform the result string as you scan left to right. It rewards a clean, mutable accumulator and careful reading of the rules. - Remove Invalid Parentheses is the hard one, and it is a genuine BFS/DFS problem wearing a string costume. You remove the minimum number of parentheses to make the string valid and return all unique valid results. The two things that separate a hire from a no-hire here are computing the minimum removals first (so you know when to stop) and deduplicating results correctly.
If you are targeting companies that lean heavily on this bucket, it is worth studying their patterns directly. Bloomberg and Microsoft both ask a lot of parsing and parentheses-style questions, and our per-company breakdowns show the actual question mix rather than a generic list.
Custom ordering and comparison
Some string problems are really about a redefined notion of order. Verifying an Alien Dictionary hands you an alphabet permutation and a list of words, and you verify the words are sorted under that custom order. The standard solution builds a rank array mapping each character to its position in the alien alphabet, then compares adjacent words character by character. The edge case candidates miss constantly: a prefix that is longer than the word it precedes ("apple" before "app") should fail. Handle the "ran out of characters" case explicitly and you will pass.
This pattern generalizes to any comparator-based question, and interviewers at companies like Google frequently push a simple comparison problem into a custom-comparator variant to see if you can adapt.
Know your language's string internals
Once your logic is correct, expect a follow-up on efficiency, and string efficiency is dominated by immutability. In Python, strings are immutable, so every time you use += to concatenate, a new string object must be created, resulting in higher processing overhead. Building a result with repeated += in a loop is quadratic; collect pieces in a list and call "".join(...) once instead.
The same rule holds in Java. Concatenating n strings with + in a loop is quadratic because each concatenation copies the whole accumulated buffer, while a reused StringBuilder gives you amortized linear time. If an interviewer asks why your reverse-a-string loop is slow, "strings are immutable so I am reallocating on every append" is the answer they are listening for. Convert to a list (Python) or char[] / StringBuilder (Java) up front, mutate freely, and materialize the string once at the end.
What candidates actually get wrong
From watching thousands of these sessions, the failure modes are boringly consistent, and none of them are about knowing an exotic algorithm:
- Silent rebuilding. Concatenating inside a loop instead of using a mutable buffer. It is the most common self-inflicted complexity wound.
- Not stating the pattern. Strong candidates say "this is a sliding window because we want the longest substring under a constraint" before writing code. It signals recognition and lets the interviewer course-correct early.
- Skipping edge cases. Empty string, single character, all-same characters, and the prefix case in ordering problems. Enumerate them out loud before you code.
- Off-by-one in pointer bounds. Especially in two-pointer swaps. Write the loop condition (
left < right) deliberately.
Drill the five patterns against the problems above, narrate your pattern choice, and handle immutability correctly, and you will clear the large majority of string questions you meet.
FAQ
What are the most common string interview questions?
They cluster into reversals and palindromes (two pointers), longest/shortest substring problems (sliding window), anagram and character-count problems (frequency maps), parentheses and path parsing (stacks), and custom-order comparisons. Concrete examples to practice include Reverse Vowels of a String, Longest Nice Substring, and Verifying an Alien Dictionary.
How do I know when to use a sliding window versus two pointers?
Use two pointers when you compare or swap from both ends, such as palindromes and reversals. Use a sliding window when you are optimizing a contiguous range against a constraint, like "longest substring with at most k distinct characters." If the validity condition is not monotonic, a plain window may not work and you should consider divide and conquer.
Why do interviewers care about string immutability?
Because it directly controls your time complexity. Building a string with += in a loop is O(n squared) in Python and Java since each operation allocates and copies a new buffer. Interviewers use this to check whether you understand what your code costs at the memory level, not just at the algorithm level.
How many string questions should I practice before an interview?
Depth beats breadth. Solving 15 to 20 problems that span all five patterns, and being able to explain each solution cleanly, prepares you better than grinding 100 near-identical ones. Focus on articulating your pattern choice and edge cases, which is what actually gets scored.