Bird
Raised Fist0
DSA Typescriptprogramming~5 mins

Longest Palindromic Substring in DSA Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a palindrome?
A palindrome is a word, phrase, or sequence that reads the same backward as forward, like 'madam' or 'racecar'.
Click to reveal answer
beginner
What does the 'Longest Palindromic Substring' problem ask for?
It asks to find the longest continuous part (substring) of a string that is a palindrome.
Click to reveal answer
beginner
How can you check if a substring is a palindrome?
Compare characters from the start and end moving towards the center. If all pairs match, it's a palindrome.
Click to reveal answer
intermediate
What is the center expansion technique in finding palindromes?
Start from each character (and between characters) and expand outwards while characters match, to find palindromes centered there.
Click to reveal answer
intermediate
Why do we check both odd and even length palindromes separately?
Odd length palindromes have a single center character, while even length palindromes have a center between two characters.
Click to reveal answer
What is the output for the longest palindromic substring in 'babad'?
A"bad"
B"ba"
C"bab" or "aba"
D"dad"
Which technique is commonly used to find the longest palindromic substring efficiently?
AHashing
BCenter expansion
CBinary search
DSorting
How many centers do we consider for a string of length n in center expansion?
An
Bn - 1
C2n
D2n - 1
What is the time complexity of the center expansion approach?
AO(n^2)
BO(n log n)
CO(n)
DO(log n)
If the input string is 'cbbd', what is the longest palindromic substring?
A"bb"
B"c"
C"bd"
D"cb"
Explain how the center expansion method finds the longest palindromic substring.
Think about growing palindromes from the middle outwards.
You got /4 concepts.
    Describe why checking both odd and even length palindromes is necessary.
    Consider palindromes like 'aba' vs 'abba'.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main goal of the Longest Palindromic Substring problem?
      easy
      A. Find the longest substring with all unique characters
      B. Find the longest substring that reads the same forwards and backwards
      C. Find the longest substring with increasing characters
      D. Find the longest substring that contains only vowels

      Solution

      1. Step 1: Understand palindrome definition

        A palindrome reads the same forwards and backwards, like 'madam' or 'racecar'.
      2. Step 2: Identify the problem goal

        The problem asks to find the longest substring inside a string that is a palindrome.
      3. Final Answer:

        Find the longest substring that reads the same forwards and backwards -> Option B
      4. Quick Check:

        Palindrome substring = longest palindrome [OK]
      Hint: Look for substrings that mirror around a center [OK]
      Common Mistakes:
      • Confusing palindrome with unique characters
      • Looking for longest substring without repeats
      • Thinking about substrings with vowels only
      2. Which of the following TypeScript function signatures correctly represents a function to find the longest palindromic substring?
      easy
      A. function longestPalindrome(s: string): string
      B. function longestPalindrome(s: number): number
      C. function longestPalindrome(s: string): number[]
      D. function longestPalindrome(s: boolean): string

      Solution

      1. Step 1: Check input parameter type

        The input is a string, so parameter type must be string.
      2. Step 2: Check return type

        The function returns the longest palindromic substring, which is a string.
      3. Final Answer:

        function longestPalindrome(s: string): string -> Option A
      4. Quick Check:

        Input string, output string [OK]
      Hint: Input and output are both strings for this problem [OK]
      Common Mistakes:
      • Using wrong input type like number or boolean
      • Returning array instead of string
      • Mixing input and output types
      3. What will be the output of the following TypeScript code?
      function longestPalindrome(s: string): string {
        let start = 0, maxLength = 1;
        for (let i = 0; i < s.length; i++) {
          let low = i, high = i;
          while (low >= 0 && high < s.length && s[low] === s[high]) {
            if (high - low + 1 > maxLength) {
              start = low;
              maxLength = high - low + 1;
            }
            low--;
            high++;
          }
        }
        return s.substring(start, start + maxLength);
      }
      
      console.log(longestPalindrome('babad'))
      medium
      A. "bab"
      B. "aba"
      C. "bad"
      D. "babad"

      Solution

      1. Step 1: Understand the code logic

        The code expands around each character to find odd-length palindromes and tracks the longest one.
      2. Step 2: Trace the input 'babad'

        Expanding around index 1 ('a') finds 'bab' (length 3). Around index 2 ('b') finds 'aba' (length 3). Both have length 3, but 'bab' is found first and stored.
      3. Final Answer:

        "bab" -> Option A
      4. Quick Check:

        First longest palindrome found = "bab" [OK]
      Hint: Expand around center and pick first longest palindrome [OK]
      Common Mistakes:
      • Assuming 'aba' is always returned instead of 'bab'
      • Returning entire string incorrectly
      • Confusing substring indices
      4. Identify the bug in the following TypeScript code for finding the longest palindromic substring:
      function longestPalindrome(s: string): string {
        let start = 0, maxLength = 1;
        for (let i = 0; i < s.length; i++) {
          let low = i, high = i + 1;
          while (low >= 0 && high < s.length && s[low] === s[high]) {
            if (high - low + 1 > maxLength) {
              start = low;
              maxLength = high - low + 1;
            }
            low--;
            high++;
          }
        }
        return s.substring(start, start + maxLength);
      }
      medium
      A. The loop should start from 1 instead of 0
      B. The substring indices are incorrect in the return statement
      C. The code only checks even-length palindromes, missing odd-length ones
      D. The variables low and high are initialized incorrectly for odd-length palindromes

      Solution

      1. Step 1: Analyze initialization of low and high

        low = i and high = i + 1 means it only checks even-length palindromes centered between i and i+1.
      2. Step 2: Identify missing odd-length palindrome check

        Odd-length palindromes centered at i (low = high = i) are not checked, so some palindromes are missed.
      3. Final Answer:

        The code only checks even-length palindromes, missing odd-length ones -> Option C
      4. Quick Check:

        Only even-length palindrome check = bug [OK]
      Hint: Check both odd (low=high=i) and even (low=i, high=i+1) centers [OK]
      Common Mistakes:
      • Checking only even-length palindromes
      • Not expanding around single character centers
      • Incorrect loop start index
      5. Given the string "forgeeksskeegfor", which approach correctly finds the longest palindromic substring and what is that substring?
      hard
      A. Manacher's algorithm; longest palindrome is "forgeeksskeegfor"
      B. Dynamic programming approach; longest palindrome is "forgeeksskeegfor"
      C. Brute force checking all substrings; longest palindrome is "ss"
      D. Expand around center approach; longest palindrome is "geeksskeeg"

      Solution

      1. Step 1: Identify the longest palindrome in the string

        The substring "geeksskeeg" is the longest palindrome inside "forgeeksskeegfor".
      2. Step 2: Match approach with correct palindrome

        Expand around center approach finds "geeksskeeg" correctly. The entire string is not a palindrome, so options A and B are incorrect. Brute force (C) incorrectly identifies "ss" as the longest.
      3. Final Answer:

        Expand around center approach; longest palindrome is "geeksskeeg" -> Option D
      4. Quick Check:

        Center expansion finds "geeksskeeg" [OK]
      Hint: Use center expansion for efficient palindrome search [OK]
      Common Mistakes:
      • Assuming entire string is palindrome
      • Confusing brute force with efficient methods
      • Mixing up palindrome substrings
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard