1. Introduction to String Interview Questions
Strings are one of the most universally tested topics in software engineering interviews. Unlike arrays, strings are immutable in many languages (like Python, Java, and JavaScript), requiring careful memory awareness.
Below are 25 essential string interview questions with complete solutions in C++, Java, Python, and JavaScript.
2. Easy String Questions
Q1. Reverse a String In-Place
Question: Given a character array s, reverse it in-place.
cppvoid reverseString(vector<char>& s) { int left = 0, right = s.size() - 1; while (left < right) swap(s[left++], s[right--]); }
javapublic void reverseString(char[] s) { int left = 0, right = s.length - 1; while (left < right) { char temp = s[left]; s[left++] = s[right]; s[right--] = temp; } }
pythondef reverseString(s: list) -> None: left, right = 0, len(s) - 1 while left < right: s[left], s[right] = s[right], s[left] left += 1; right -= 1
javascriptfunction reverseString(s) { let left = 0, right = s.length - 1; while (left < right) { let temp = s[left]; s[left++] = s[right]; s[right--] = temp; } }
Time Complexity: O(n) | Space Complexity: O(1)
Q2. Valid Palindrome
Question: Return true if a string is a palindrome, considering only alphanumeric characters and ignoring cases.
cppbool isPalindrome(string s) { int left = 0, right = s.size() - 1; while (left < right) { while (left < right && !isalnum(s[left])) left++; while (left < right && !isalnum(s[right])) right--; if (tolower(s[left]) != tolower(s[right])) return false; left++; right--; } return true; }
javapublic boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++; while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--; if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) return false; left++; right--; } return true; }
pythondef isPalindrome(s: str) -> bool: left, right = 0, len(s) - 1 while left < right: while left < right and not s[left].isalnum(): left += 1 while left < right and not s[right].isalnum(): right -= 1 if s[left].lower() != s[right].lower(): return False left += 1; right -= 1 return True
javascriptfunction isPalindrome(s) { let left = 0, right = s.length - 1; while (left < right) { while (left < right && !/[a-zA-Z0-9]/.test(s[left])) left++; while (left < right && !/[a-zA-Z0-9]/.test(s[right])) right--; if (s[left].toLowerCase() !== s[right].toLowerCase()) return false; left++; right--; } return true; }
Time Complexity: O(n) | Space Complexity: O(1)
Q3. Valid Anagram
Question: Return true if t is an anagram of s.
cppbool isAnagram(string s, string t) { if (s.size() != t.size()) return false; int count[26] = {0}; for (char c : s) count[c - 'a']++; for (char c : t) if (--count[c - 'a'] < 0) return false; return true; }
javapublic boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; int[] count = new int[26]; for (char c : s.toCharArray()) count[c - 'a']++; for (char c : t.toCharArray()) if (--count[c - 'a'] < 0) return false; return true; }
pythonfrom collections import Counter def isAnagram(s: str, t: str) -> bool: return Counter(s) == Counter(t)
javascriptfunction isAnagram(s, t) { if (s.length !== t.length) return false; let count = new Array(26).fill(0); for (let c of s) count[c.charCodeAt(0) - 97]++; for (let c of t) { let idx = c.charCodeAt(0) - 97; count[idx]--; if (count[idx] < 0) return false; } return true; }
Time Complexity: O(n) | Space Complexity: O(1)
Q4. Longest Common Prefix
cppstring longestCommonPrefix(vector<string>& strs) { if (strs.empty()) return ""; string prefix = strs[0]; for (int i = 1; i < strs.size(); i++) { while (strs[i].find(prefix) != 0) { prefix = prefix.substr(0, prefix.size() - 1); if (prefix.empty()) return ""; } } return prefix; }
javapublic String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return ""; } } return prefix; }
pythondef longestCommonPrefix(strs: list) -> str: if not strs: return "" prefix = strs[0] for s in strs[1:]: while not s.startswith(prefix): prefix = prefix[:-1] if not prefix: return "" return prefix
javascriptfunction longestCommonPrefix(strs) { if (!strs || strs.length === 0) return ""; let prefix = strs[0]; for (let i = 1; i < strs.length; i++) { while (strs[i].indexOf(prefix) !== 0) { prefix = prefix.substring(0, prefix.length - 1); if (!prefix) return ""; } } return prefix; }
Time Complexity: O(S) | Space Complexity: O(1)
3. Summary Table
| Problem | Technique | Time | Space |
|---|---|---|---|
| Reverse String | Two Pointers | O(n) | O(1) |
| Valid Palindrome | Two Pointers | O(n) | O(1) |
| Valid Anagram | Frequency Counter | O(n) | O(1) |
| Longest Common Prefix | Prefix Reduction | O(S) | O(1) |
Practice all string problems on DSAMaster's practice platform.
