Bird
Raised Fist0
DSA Typescriptprogramming~30 mins

Longest Palindromic Substring in DSA Typescript - Build from Scratch

Choose your learning style10 modes available
Longest Palindromic Substring
📖 Scenario: Imagine you are building a text analysis tool that finds the longest palindrome inside a given word or phrase. Palindromes are words or sequences that read the same backward as forward, like "racecar" or "level".
🎯 Goal: Build a TypeScript program that finds the longest palindromic substring inside a given string.
📋 What You'll Learn
Create a string variable with a specific value
Create helper variables to track palindrome boundaries
Implement a function to expand around the center and find palindromes
Use a loop to check all possible centers for palindromes
Print the longest palindromic substring found
💡 Why This Matters
🌍 Real World
Finding palindromic substrings is useful in DNA sequence analysis, text processing, and data compression.
💼 Career
Understanding string manipulation and palindrome detection is important for software engineers working on search engines, bioinformatics, and text editors.
Progress0 / 4 steps
1
DATA SETUP: Create the input string
Create a string variable called input and set it to the value "babad".
DSA Typescript
Hint

Use const input: string = "babad"; to create the string.

2
CONFIGURATION: Create variables to track palindrome boundaries
Add two number variables called start and end and set both to 0.
DSA Typescript
Hint

Use let start: number = 0; and let end: number = 0; to track palindrome boundaries.

3
CORE LOGIC: Implement the palindrome expansion function and main loop
Write a function called expandAroundCenter that takes two numbers left and right and returns the length of the palindrome by expanding around these centers. Then, use a for loop with variable i from 0 to input.length - 1 to find the longest palindrome by checking odd and even length palindromes. Update start and end when a longer palindrome is found.
DSA Typescript
Hint

Define expandAroundCenter to check palindrome length. Use a for loop with i to check all centers.

4
OUTPUT: Print the longest palindromic substring
Print the substring of input from start to end + 1 using console.log.
DSA Typescript
Hint

Use console.log(input.substring(start, end + 1)); to print the longest palindrome.

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