Longest Palindromic Substring Problem Statement

You are provided with a string STR of length N. The task is to find the longest palindromic substring within STR. If there are several palindromic substrings of the maximum length, return the one that starts with the smallest index.

Input:

The first line of the input contains a single integer T, the number of test cases.
Each of the following T lines contains a single string STR.

Output:

For each test case, output the longest palindromic substring. If multiple substrings qualify, return the one that appears first.

Example:

Input:
2
ababc
cbbd
Output:
aba
bb
Explanation:

For first test case, "aba" and "bab" are both palindromes. "aba" is chosen because it starts earlier.

Constraints:

  • 1 ≤ T ≤ 5
  • 0 ≤ N ≤ 100
Note:

A substring is defined as a contiguous segment of a string.

AnswerBot
4mo

Given a string, find the longest palindromic substring within it, returning the one that starts with the smallest index if multiple substrings qualify.

  • Iterate through the string and expand around each...read more

Help your peers!
Select
Add answer anonymously...
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits