Return a Longest Palindromic Slice

Quick Overview

Find a longest contiguous palindrome in an alphanumeric string. Explore center expansion, tied answers, correctness, edge cases, and time and space costs.

Return a Longest Palindromic Slice

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Given the string `text`, return a contiguous substring of maximum length whose characters read identically from left to right and from right to left. If several substrings reach that maximum length, any one is acceptable. ### Examples **Example 1** ```text Input: text = "racecarzabba" Output: "racecar" ``` The seven-character palindrome is longer than the four-character palindrome near the end. **Example 2** ```text Input: text = "abaxyzcdc" Output: "cdc" ``` The substring `"aba"` is another acceptable answer: both have length three, and neither can be extended to a longer palindrome in this input. ### Constraints & Assumptions - The string contains between 1 and 1,000 characters, inclusive. - Its characters are English letters and decimal digits. - The returned characters must occupy consecutive positions in the input. A subsequence assembled from separated positions does not qualify. - Compare the characters as given; no case folding or character filtering is requested. - No tie-breaking rule or particular algorithm is required. ### Clarifying Questions to Ask - When two different palindromes have the same maximum length, can either be returned? Yes; there is no preference for the earlier occurrence or a particular spelling. - Does a one-character substring qualify? Yes, including when no longer palindrome exists. ```hint Check the boundary cases An optimal answer can have either an odd or an even number of characters. A method that handles only one of those shapes is incomplete. ``` ### What a Strong Answer Covers - A method that considers both odd-length and even-length palindromes while returning an actual substring. - A reason that every possible longest palindrome is represented among the cases examined. - Correct handling of repeated characters, single-character inputs, and tied maximum lengths. - Time and auxiliary-space bounds, with the returned string accounted for separately. ### Follow-up Questions - What extra state would a substring dynamic-programming approach store, and how would its costs compare? - Can repeated comparisons be shared to obtain a linear-time method? Describe the key reuse rule without imposing a tie-break on the answer.

Overview: Find a longest contiguous palindrome in an alphanumeric string. Explore center expansion, tied answers, correctness, edge cases, and time and space costs.

Amazon logo
Amazon
Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given the string text, return a contiguous substring of maximum length whose characters read identically from left to right and from right to left. If several substrings reach that maximum length, any one is acceptable.

Examples

Example 1

Input: text = "racecarzabba"
Output: "racecar"

The seven-character palindrome is longer than the four-character palindrome near the end.

Example 2

Input: text = "abaxyzcdc"
Output: "cdc"

The substring "aba" is another acceptable answer: both have length three, and neither can be extended to a longer palindrome in this input.

Constraints & Assumptions

  • The string contains between 1 and 1,000 characters, inclusive.
  • Its characters are English letters and decimal digits.
  • The returned characters must occupy consecutive positions in the input. A subsequence assembled from separated positions does not qualify.
  • Compare the characters as given; no case folding or character filtering is requested.
  • No tie-breaking rule or particular algorithm is required.

Clarifying Questions to Ask Guidance

  • When two different palindromes have the same maximum length, can either be returned? Yes; there is no preference for the earlier occurrence or a particular spelling.
  • Does a one-character substring qualify? Yes, including when no longer palindrome exists.

What a Strong Answer Covers Guidance

  • A method that considers both odd-length and even-length palindromes while returning an actual substring.
  • A reason that every possible longest palindrome is represented among the cases examined.
  • Correct handling of repeated characters, single-character inputs, and tied maximum lengths.
  • Time and auxiliary-space bounds, with the returned string accounted for separately.

Follow-up Questions Guidance

  • What extra state would a substring dynamic-programming approach store, and how would its costs compare?
  • Can repeated comparisons be shared to obtain a linear-time method? Describe the key reuse rule without imposing a tie-break on the answer.