18. Longest Palindromic Substring

18. Longest Palindromic Substring

Medium
41.0%
1.7k
stringdynamic-programmingtwo-pointers
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large messaging, search, or content moderation platform analyzes user-generated text to detect patterns like mirrored phrases, repeated sequences, or symmetric tokens. These patterns can help:

  • identify spam campaigns
  • detect automated bot behavior
  • find repeated templates in messages
  • improve search relevance using symmetric phrases

The platform represents a text record as a string s. Your task is to find the longest contiguous segment of text that reads the same forward and backward.

This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given a string s, return the longest palindromic substring in s.

A palindrome is a string that reads the same forward and backward.
A substring must be contiguous.

If multiple answers have the same maximum length, return any one of them.

Input Format

  • s: a string

Output Format

  • A string representing the longest palindromic substring

Examples

Example 1:

Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer because it has the same maximum length.

Example 2:

Input: s = "cbbd"
Output: "bb"

Example 3:

Input: s = "a"
Output: "a"

Constraints

  • 1 <= s.length <= 1000
  • s consists of English letters, digits, and common symbols

Loading editor...

"babad"
"bab"

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback