Longest Palindromic Substring Problem Statement

You are provided with a string STR of length N. The goal is to identify the longest palindromic substring within this string. In cases where multiple palindromic substrings have maximum lengths, return the substring which begins at the smaller index.

Input:

The first line consists of an integer 'T', indicating the number of test cases.
Each test case contains one line, a string 'STR'.

Output:

For each test case, print the longest palindromic substring on a separate line.
If there is more than one, choose the one with the smallest start index.

Example:

Input:
T = 1
STR = "ababc"
Output:
"aba"
Explanation:

The longest palindromic substring of "ababc" is "aba". Although "bab" is also a palindrome of the same length (3), "aba" starts earlier.

Constraints:

  • 1 <= T <= 10
  • 0 <= N <= 10^3
  • Each 'STR' is composed of lowercase English letters.
  • Time Limit: 1 second
AnswerBot
4mo

Given a string, find the longest palindromic substring, prioritizing the one with the smallest start index.

  • Iterate through each character in the string and expand around it to find palindromes

  • Keep tra...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. For a given array, how would you count the number of inversions?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ2. For a given array, how would you count the number of inversions?
A Software Developer Intern was asked 4mo agoQ3. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Amazon Software Developer Intern 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