Strings Interview Questions & Tips
What are Strings?
Along with arrays, strings are one of the simplest data structures out there and are always coming up. Strings can be thought of as a subset of arrays because, as simple as they are, strings come with their own set of intricacies and challenges. Here are three key properties that we need to remember as we start working with strings.
- Under the hood, strings are just an array of characters. This is important because it affects the time complexities of string methods we might use – keep reading for more details on this!
- Like arrays, strings are variable in size. This one might be obvious given the above point, but it bears repeating. In most languages, arrays are treated as a collection, whereas we think of a string as a single “thing”. However, remember that it’s still an array of characters in memory, so the space the string takes up AND it takes a linear amount of time to generate a hash of a string where N is proportional to the length of the string. When discussing time complexities the length of a string plays a factor.
- In most languages, strings are immutable. This is the biggest difference between strings and generic arrays – you can't change them once they are constructed! If you need to update a string you have to rebuild an entirely new string with the update you want. How you avoid this waste of time & space is language dependent. In Java, a StringBuilder is used to hold a stream of characters before converting it to a final immutable string. In Python, it's common to use a list to hold individual characters and then convert it to a string at the very end. Some problems like Reversing a String are commonly solved in C++, where strings are mutable, but if you are working in a language where strings aren't mutable to follow the spirit of this question it requires you to "fake" string mutability by first converting your string to a list then pretending your language can mutate strings directly.
Common Mistakes in Interviews Featuring Strings
Viewing String Problems As "Easy"
Strings, despite being one of the first data structures introduced to programmers, shouldn't be underestimated as "easy" in interview scenarios due to their versatility. Unlike data structures like trees or graphs that have specific algorithms like DFS/BFS or backtracking respectively, strings can incorporate a range of technical topics frequently asked in interviews. Strings are a linear data structure, and because of that all common algorithms related to linear data structures could potentially be involved in a string question. This means techniques like two pointers, sliding windows, recursion, backtracking, and dynamic programming (to name just a few) can be used in a string question. Therefore, avoiding string practice due to perceived ease could lead to challenges in handling complex string problems in interviews.
Messing Up String Conversions
One of the more annoying things we can have to do, which nevertheless comes up fairly often, is various forms of string math. This includes converting characters to their index in the alphabet and converting characters to their numeric value. General familiarity with how to do these things in your interviewing language is critical. It's obscure enough to not remember on the spot, but simple enough that it looks bad if you don't know how to do it.
Making Assumptions About String Contents
It's easy to think of strings as just letters in the alphabet, but this will lead to one of the most common interview errors with string questions – making an incorrect assumption about the string contents. Useful clarifying questions to ask whenever strings are involved in a problem can be questions like.
- Are we guaranteed that there will just be alphabetical characters? Alphanumeric?
- Do we need to worry about punctuation?
- Do I need to handle special characters/symbols?
- Can the string ever be empty or null?
- Is the string always lower/uppercase? Can we receive a mixed case string?
- Do we need to worry about the string encoding at all? UTF-8, ASCII, etc?
- Does the string fit into memory?
Here's a good example of a string question where all of these questions matter in achieving the correct output to pass all tests. Don't make assumptions about what is in the string, asking these types of questions helps demonstrate your seniority and familiarity with common gotchas.
Hidden Complexity
One danger you should be aware of is how languages will abstract away the heavy lifting when it comes to strings. This can make us wrongly assume that "simple" operations are cheap. For example, we can very easily get a substring of a string in Python using the Python slicing syntax: s[1:5]. Writing it this way we may assume that this is a constant-time operation. However, in most cases, operations like this are still going to copy the substring (remember strings are immutable), meaning that it is going to take us linear time.
One great feature of Java is the StringBuilder class. This class provides a performant way to modify and append onto a string by using an array as the underlying data structure. StringBuilder also provides a nice toString() method that we can use to recover a string.
C uses an array as the underlying data structure which prevents the language from masking additional complexity. With that said, we have to be careful when allocating an array in C. The size of the array is static which means all of the memory needs to be allocated up front!
String Operation | Description | Time | Space |
---|---|---|---|
Length | Returns the number of characters in the string. | O(1)
| O(1)
|
Index Access | Accesses a character at a specific position in the string. | O(1)
| O(1)
|
Concatenation (+ )
| Combines two strings into one. | O(n)
| O(n)
|
Traversal (e.g. for loop)
| Goes through each character in the string one by one. | O(n)
| O(1)
|
Naive Search (e.g. contains , index of )
| Searches for a specific character or substring in the string. | O(n*m)
| O(1)
|
Substring | Creates a new string that is a subset of the original string. | O(m)
| O(m)
|
n
represents the length of the stringm
represents the length of the substring
This above table is a generalized view of string operations, but it's worth mentioning that the details will differ depending on your particular language. For example, in some Java versions, the substring operation was O(1)
in space complexity as it shared the same character array with the original string, but this has been changed in more recent versions to avoid memory leaks, leading to O(s)
space complexity, with m
as the length of the substring.
Advanced String Topics
The Multiple Pointers Technique
Same as with arrays, it is common to use multiple pointers to traverse a string. This allows us to accomplish various interesting tasks with substrings and comparing characters. Here are some problems to consider:
The Sliding Window Technique
Sliding windows are common with any linear data structure. That includes things like arrays, linked lists, and, of course, strings. They are a great tool for finding the longest substring matching an arbitrary property. For practice take a look at the following.
- Longest substring without repeating characters
- Longest substring with at least k repeating characters
- Find all anagrams in a string
Advanced String Algorithms
These advanced algorithms are fairly niche and only likely to be asked at FAANG companies, and even then these algorithms show up a small fraction of the time. If you want to go deep on strings, there are three advanced algorithms worth having on hand. All of these algorithms are used for searching for a substring within a string and variations of them exist in a handful of problems online. The core algorithms to learn are:
Regular Expressions
While it is unlikely that you will get a problem which can be solved directly with regular expressions, you should have a good idea of how regular expressions work. Leetcode questions with regular expression answers do exist, but are uncommon. If you're thinking of using regular expressions in your solution, you're probably over-complicating it. With that stated, it is still important to have familiarity with them. Consider playing with a site like regex101.com to develop a better sense of regular expressions. Additionally, spend some time thinking through how to document a regular expression. Demonstrating not just an understanding of regular expressions but how to make them maintainable shows a certain coding maturity.
Language-Specific Advice
Up to this point, all the advice we have given has been language agnostic but various languages handle strings very differently. In this section, we will cover features of four popular language choices: Java, C++, Python & JavaScript.
Java Strings
Fast Facts:
- Mutable? No
- Primitive? No
- Comparison:
s1.equals(s2)
- Access the ith character:
s1.charAt(i)
Useful Java String Methods:
Method | Description | Time | Space |
---|---|---|---|
s.length()
| Returns the length of the string | O(1)
| O(1)
|
s.charAt(int i)
| Returns the character at index i
| O(1)
| O(1)
|
s.substring(int i, int j)
| Returns substring from i to j (inclusive of i , exclusive of j )
| O(j-i)
| O(j-i)
|
s.contains(String s)
| Returns True if s is contained in the string
| O(1)
| O(1)
|
s.indexOf(String s)
| Returns the starting index of the first occurrence of s
| O(n)
| O(1)
|
In Java, there are some common points to be aware of…
/* Strings are OBJECTS */
// Primitives in Java are lowercase and include data types like:
double foo = 76.762121;
char bar = 'i';
boolean baz = true;
// Strings are *Objects*
String s = "Interviewing.io";
/* String comparison doesn't use the == operator */
String a = "Interview";
String b = "Interview";
// This outputs false because == compares the Object pointers not values
System.out.println(a == b);
// This outputs true because equals() compares the values of the strings
System.out.println(a.equals(b));
/* Comparing Object pointers uses the == operator */
String c = a;
// This outputs true since a and b point to the same object in memory
System.out.println(a == c);
1/* Strings are OBJECTS */
2// Primitives in Java are lowercase and include data types like:
3double foo = 76.762121;
4char bar = 'i';
5boolean baz = true;
6
7// Strings are *Objects*
8String s = "Interviewing.io";
9
10
11/* String comparison doesn't use the == operator */
12String a = "Interview";
13String b = "Interview";
14
15// This outputs false because == compares the Object pointers not values
16System.out.println(a == b);
17// This outputs true because equals() compares the values of the strings
18System.out.println(a.equals(b));
19
20
21/* Comparing Object pointers uses the == operator */
22String c = a;
23// This outputs true since a and b point to the same object in memory
24System.out.println(a == c);
C++ Strings
Fast Facts:
- Mutable? No
- Primitive? No
- Comparison:
s1.compare(s2)
- Access the ith character:
s1[i]
Useful C++ String Methods:
Method | Description | Time | Space |
---|---|---|---|
s.length()
| Returns the length of the string s (from string::length)
| O(1)
| O(1)
|
s1.find(s2)
| Returns the index of s1 in the string s2 (from string::find)
| O(s1 * s2)
| O(1)
|
strcpy(chars, s.c_str())
| Converts s1 into a character array
| O(n)
| O(n)
|
s.substr(i, j)
| Get the substring of s from i with length j
| O(j)
| O(j)
|
The largest difference between strings in Java and C++ is that strings are mutable in C++. See below for details!
#include <iostream>
#include <string>
int main() {
// Primitive types
double foo = 76.762121;
char bar = 'i';
bool baz = true;
// Strings are not a primitive in C++. They are an object of std::string class
std::string a = "Interviews";
std::string b = "Interviews";
// Comparing strings using compare()
// Returns 0 since strings are equal
if(a.compare(b) == 0){
std::cout << "You will see this because both strings are identical";
}
// Comparing strings using == operator
if(a == b){
std::cout << "You will see this because both strings are identical";
}
// Strings are mutable in C++
std::string c = str1;
c[9] = 'z'; // attempt to modify the last letter of string works!
std::cout << c; // will print "Interviewz"
return 0;
}
1#include <iostream>
2#include <string>
3
4int main() {
5 // Primitive types
6 double foo = 76.762121;
7 char bar = 'i';
8 bool baz = true;
9
10 // Strings are not a primitive in C++. They are an object of std::string class
11 std::string a = "Interviews";
12 std::string b = "Interviews";
13
14 // Comparing strings using compare()
15 // Returns 0 since strings are equal
16 if(a.compare(b) == 0){
17 std::cout << "You will see this because both strings are identical";
18 }
19
20 // Comparing strings using == operator
21 if(a == b){
22 std::cout << "You will see this because both strings are identical";
23 }
24
25 // Strings are mutable in C++
26 std::string c = str1;
27
28 c[9] = 'z'; // attempt to modify the last letter of string works!
29 std::cout << c; // will print "Interviewz"
30
31 return 0;
32}
Python Strings
Fast Facts:
- Mutable? No
- Primitive? Yes and no, but mostly no. "Primitive" isn't a word in Python, all types are objects!
- Comparison:
s1 == s2
- Access the ith character:
s1[i]
Useful Python String Methods:
Method | Description | Time | Space |
---|---|---|---|
len()
| Returns the length of the string | O(1)
| O(1)
|
s1 in s2
| Is s1 a substring of s2
| O(s1 * s2)
| O(1)
|
s1.index(s2)
| Returns the index of the first occurrence of s2 in the string, ValueError if not found
| O(s1)
| O(1)
|
list(s)
| Converts s1 into a list of characters
| O(n)
| O(n)
|
s[i:j]
| Get the substring of s from i (inclusive) to j (exclusive)
| O(j-i)
| O(j-i)
|
In typical Python fashion, this is probably the easiest of these four languages to handle strings. Rather than having to use a lot of function calls, much of what we might want to do is built into the language, such as getting substrings of a string. However, you should use caution when using Python, the simplicity of the language often masks underlying complexity.
# All types in Python are objects
foo = 76.762121
bar = 'i'
baz = True
a = "Interviews"
b = "Interviews"
print(type(foo)) # <class 'float'>
print(type(bar)) # <class 'str'>
print(type(baz)) # <class 'bool>
print(type(a)) # <class 'str>
# Comparing strings in Python
print(a == b) # True
# Strings are immutable in Python
a[9] = 'z' # TypeError: 'str' object does not support item assignment
1# All types in Python are objects
2foo = 76.762121
3bar = 'i'
4baz = True
5a = "Interviews"
6b = "Interviews"
7print(type(foo)) # <class 'float'>
8print(type(bar)) # <class 'str'>
9print(type(baz)) # <class 'bool>
10print(type(a)) # <class 'str>
11
12# Comparing strings in Python
13print(a == b) # True
14
15# Strings are immutable in Python
16a[9] = 'z' # TypeError: 'str' object does not support item assignment
JavaScript Strings
Fast Facts:
- Mutable? No
- Primitive? Yes
- Comparison:
s1 == s2
and sometimess1 === s2
(see below for details) - Access the ith character:
s1[i]
Useful JavaScript String Methods:
Method | Description | Time | Space |
---|---|---|---|
s.length()
| Returns the length of the string | O(1)
| O(1)
|
s2.includes(s1)
| Is s1 a substring of s2
| O(s2)
| O(1)
|
s.indexOf(searchValue)
| Returns the index of the first occurrence of searchValue
| O(1)
| O(1)
|
s.split(separator)
| Splits the string into an array of substrings based on the separator | O(1)
| O(1)
|
s.join(separator)
| Joins the elements of an array into a string using the separator | O(1)
| O(1)
|
s.substring(i, j)
| Get the substring of s from i (inclusive) to j (exclusive)
| O(1)
| O(1)
|
JavaScript strings share some similarities with other languages but also have their unique characteristics. In JavaScript, strings are primitive values but have access to several built-in methods that allow manipulation and retrieval of string data. It's important to note that JavaScript strings are immutable, meaning that once a string is created, it cannot be changed. Instead, operations on strings return new strings.
When comparing strings in JavaScript, the strict equality operator (===)
is used to compare both the value and the type of the string. This ensures an accurate comparison of two strings. To access individual characters within a string, the charAt()
method is used.
JavaScript provides various useful methods for working with strings, such as length
to retrieve the length of a string, substring
to extract a portion of a string, includes
to check if a substring is present, indexOf
to find the index of a substring, split
to split a string into an array based on a separator, and join
to concatenate array elements into a single string using a separator.
Be sure to be particularly mindful of string immutability in JavaScript, since string update operations will compile successfully and fail silently like the below example illustrates:
let str = "ab";
// attempting to swap 'a' and 'b'
let temp = str[0]
str[0] = str[1]; // operation does nothing and fails silently!
str[1] = temp; // operation does nothing and fails silently!
console.log(str); // still "ab"!!!
1let str = "ab";
2
3// attempting to swap 'a' and 'b'
4let temp = str[0]
5str[0] = str[1]; // operation does nothing and fails silently!
6str[1] = temp; // operation does nothing and fails silently!
7console.log(str); // still "ab"!!!
Overall, JavaScript offers a range of methods and functionalities for handling strings, making it a versatile language for string manipulation tasks.
Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Longest Common Subsequence
Given two strings, return the longest common subsequence between the two strings.
Reverse String
Write a program to reverse the given string.
Decode String
Given an encoded string, return its decoded string.
Reverse Words in a String
Given an input string `s`, reverse the order of the words without reversing the words themselves.
Reverse Integer
Given a 32-bit signed integer, reverse digits of the integer.
Generate Parentheses
Given `n` pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Simplify Path
You are given a path to a file as a string. The path can contain the symbols: “..” for the parent directory and “.” for the current directory. Convert the path into its simplified form.
Valid Palindrome
Determine if this string, after removing any one character, can become a palindrome. If possible return true, otherwise return false.
Minimum Window Substring
Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window.
XML Parser
Write an XML parser and formatter.
Longest Palindromic Substring
Given a string s, return the longest palindromic substring in s.
Permutation in String
Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
Shuffle String
Write a function that takes a string as an input and returns a shuffled version of that string then write another function to analyze how well it was shuffled.
Regular Expression Matching
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element.
Longest Substring with At Most K Distinct Characters
Given a string, find the length of the longest substring in it with no more than K distinct characters.
Prefix Pairs
Given a list of words, match all words with other words from the list that are a prefix for the word.
Infinite Binary Print
Print out all numbers in binary, preserving leading zeros.
Minimum Cost to Construct String
Given a 2-D integer array mapping the letters ABCD and their costs. Calculate the smallest cost to make a string of length n.
About the Authors

Jared is a software engineer with nine years of development experience. Jared has worked in a variety of fields including power systems engineering and actuarial science. Jared is currently working at Dropbox in data infrastructure.

The Mighty Anomaly (Mike) is one of the top Google coaches for interviewing.io having personally helped over 100+ engineers get into Google with his unique coaching style. After receiving offers from the full FAANG gauntlet early in his career, he enjoys teaching others proven techniques to pass technical interviews with his decade of coaching and industry experience.

About interviewing.io
interviewing.io is a mock interview practice platform. We've hosted over 100K mock interviews, conducted by senior engineers from FAANG & other top companies. We've drawn on data from these interviews to bring you the best interview prep resource on the web.