Episode 7 — DSA with JavaScript / 7.5 — Strings

7.5 — Interview Questions: Strings

<< Overview


Beginner

Q1. How do you reverse a string?

Answer:

function reverse(s) {
    return s.split("").reverse().join("");
}
// Or with two pointers on array of chars
string reverse(string s) {
    std::reverse(s.begin(), s.end());
    return s;
}

Time: O(n) | Space: O(n) for new string


Q2. How do you check if a string is a palindrome?

Answer: Compare characters from both ends moving inward:

function isPalin(s) {
    s = s.toLowerCase().replace(/[^a-z0-9]/g, "");
    let l = 0, r = s.length - 1;
    while (l < r) { if (s[l++] !== s[r--]) return false; }
    return true;
}

Q3. What is the difference between slice and substring?

Answer:

  • slice(start, end): Supports negative indices, returns empty if start > end
  • substring(start, end): No negative indices, swaps if start > end

Q4. How are strings immutable in JavaScript?

Answer: Once created, a string's characters cannot be changed. Operations like toUpperCase() return a new string. s[0] = 'X' has no effect.


Q5. How do you count character frequency?

Answer:

function freq(s) {
    const map = {};
    for (const c of s) map[c] = (map[c] || 0) + 1;
    return map;
}

Intermediate

Q6. Check if two strings are anagrams.

Answer: Sort both and compare, or use frequency counting (O(n)):

function isAnagram(a, b) {
    if (a.length !== b.length) return false;
    const f = {};
    for (const c of a) f[c] = (f[c] || 0) + 1;
    for (const c of b) { if (!f[c]) return false; f[c]--; }
    return true;
}

Q7. Find the longest substring without repeating characters.

Answer: Sliding window with a Set:

function longestUnique(s) {
    const set = new Set();
    let l = 0, max = 0;
    for (let r = 0; r < s.length; r++) {
        while (set.has(s[r])) set.delete(s[l++]);
        set.add(s[r]);
        max = Math.max(max, r - l + 1);
    }
    return max;
}

Time: O(n) | Space: O(min(n, alphabet))


Q8. Find the first non-repeating character.

Answer:

function firstUniq(s) {
    const f = {};
    for (const c of s) f[c] = (f[c] || 0) + 1;
    for (const c of s) if (f[c] === 1) return c;
    return null;
}

Q9. Implement string compression.

Answer: "aabcccccaaa""a2b1c5a3". Return original if compressed isn't shorter.

function compress(s) {
    let res = "", i = 0;
    while (i < s.length) {
        let j = i;
        while (j < s.length && s[j] === s[i]) j++;
        res += s[i] + (j - i);
        i = j;
    }
    return res.length < s.length ? res : s;
}

Q10. Find the longest common prefix.

Answer: Compare character by character vertically:

function lcp(strs) {
    if (!strs.length) return "";
    for (let i = 0; i < strs[0].length; i++) {
        for (let j = 1; j < strs.length; j++) {
            if (i >= strs[j].length || strs[j][i] !== strs[0][i])
                return strs[0].slice(0, i);
        }
    }
    return strs[0];
}

Advanced

Q11. What is KMP string matching?

Answer: KMP preprocesses the pattern into a "failure function" (LPS array) that tells how many characters to skip on mismatch, achieving O(n+m) matching.


Q12. Find all anagrams of a pattern in a text.

Answer: Use sliding window of size p.length with frequency comparison:

function findAnagrams(s, p) {
    const res = [], pFreq = {}, wFreq = {};
    for (const c of p) pFreq[c] = (pFreq[c] || 0) + 1;
    for (let i = 0; i < s.length; i++) {
        wFreq[s[i]] = (wFreq[s[i]] || 0) + 1;
        if (i >= p.length) {
            const old = s[i - p.length];
            wFreq[old]--;
            if (wFreq[old] === 0) delete wFreq[old];
        }
        if (i >= p.length - 1) {
            if (JSON.stringify(wFreq) === JSON.stringify(pFreq)) res.push(i - p.length + 1);
        }
    }
    return res;
}

Q13. Longest palindromic substring.

Answer: Expand around center for each position:

function longestPalindrome(s) {
    let start = 0, maxLen = 1;
    function expand(l, r) {
        while (l >= 0 && r < s.length && s[l] === s[r]) { l--; r++; }
        if (r - l - 1 > maxLen) { start = l + 1; maxLen = r - l - 1; }
    }
    for (let i = 0; i < s.length; i++) { expand(i, i); expand(i, i + 1); }
    return s.slice(start, start + maxLen);
}

Time: O(n²) | Space: O(1)


Q14. Edit distance between two strings.

Answer: Dynamic programming — dp[i][j] = min operations to convert s1[0..i-1] to s2[0..j-1]:

function editDistance(s1, s2) {
    const m = s1.length, n = s2.length;
    const dp = Array.from({length: m+1}, () => Array(n+1).fill(0));
    for (let i = 0; i <= m; i++) dp[i][0] = i;
    for (let j = 0; j <= n; j++) dp[0][j] = j;
    for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
            if (s1[i-1] === s2[j-1]) dp[i][j] = dp[i-1][j-1];
            else dp[i][j] = 1 + Math.min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]);
        }
    }
    return dp[m][n];
}

Quick-fire table

#QuestionAnswer
1Reverse string?split/reverse/join or two pointers
2Palindrome check?Two pointers from ends
3Anagram check?Frequency map comparison O(n)
4Strings immutable in JS?Yes, all operations create new strings
5slice vs substring?slice supports negative, substring swaps
6First unique char?Two-pass frequency count
7KMP complexity?O(n+m) with LPS preprocessing
8Longest unique substring?Sliding window + Set
9String compression?Run-length encoding
10Edit distance?DP table O(m×n)

Rapid self-check cards

SC-001

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-002

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-003

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-004

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-005

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-006

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-007

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-008

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-009

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-010

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-011

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-012

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-013

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-014

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-015

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-016

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-017

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-018

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-019

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-020

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-021

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-022

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-023

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-024

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-025

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-026

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-027

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-028

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-029

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-030

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-031

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-032

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-033

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-034

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-035

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-036

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-037

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-038

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-039

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-040

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-041

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-042

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-043

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-044

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-045

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-046

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-047

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-048

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-049

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-050

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-051

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-052

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-053

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-054

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-055

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-056

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-057

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-058

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-059

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-060

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-061

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-062

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-063

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-064

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-065

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-066

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-067

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-068

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-069

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-070

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-071

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-072

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-073

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-074

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-075

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-076

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-077

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-078

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-079

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-080

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-081

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-082

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-083

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-084

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-085

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-086

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-087

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-088

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-089

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-090

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-091

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-092

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-093

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-094

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-095

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-096

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-097

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-098

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-099

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-100

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-101

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-102

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-103

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-104

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-105

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-106

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-107

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-108

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-109

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-110

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-111

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-112

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-113

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-114

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-115

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-116

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-117

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-118

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-119

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-120

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-121

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-122

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-123

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-124

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-125

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-126

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-127

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-128

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-129

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-130

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-131

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-132

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-133

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-134

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-135

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-136

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-137

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-138

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-139

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-140

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-141

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-142

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-143

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-144

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-145

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-146

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-147

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-148

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-149

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-150

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-151

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-152

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-153

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-154

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-155

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-156

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-157

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-158

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-159

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-160

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-161

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-162

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-163

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-164

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-165

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-166

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-167

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-168

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-169

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-170

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-171

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-172

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-173

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-174

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-175

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-176

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-177

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-178

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-179

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-180

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-181

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-182

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-183

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-184

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-185

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-186

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-187

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-188

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-189

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-190

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings

SC-191

  • Q: How to reverse a string in JS?
  • A: s.split('').reverse().join('')

SC-192

  • Q: What is a palindrome?
  • A: Reads the same forwards and backwards

SC-193

  • Q: How to check anagram?
  • A: Sort both or compare frequency maps

SC-194

  • Q: Are JS strings mutable?
  • A: No — all operations create new strings

SC-195

  • Q: Difference between slice and substring?
  • A: slice supports negative indices

SC-196

  • Q: How to find first unique char?
  • A: Two-pass: build frequency map, then scan

SC-197

  • Q: What is KMP algorithm?
  • A: Pattern matching in O(n+m) using LPS array

SC-198

  • Q: Sliding window for strings?
  • A: Move window, track chars with Set or Map

SC-199

  • Q: What is run-length encoding?
  • A: Replace consecutive chars with char+count

SC-200

  • Q: Edit distance approach?
  • A: DP table comparing prefixes of both strings