FastPrepLongest Palindromic Substring
Problems/Longest Palindromic Substring

Longest Palindromic Substring

Siemens logoSiemens● MediumINTERNPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interviewer asked for the longest palindromic substring. The candidate wrote a brute-force answer first, then explained the faster idea of growing a palindrome out from its center, without coding it.

The report does not give limits or say which answer to return when two are equally long. Here, the string has 1 to 1000 letters and digits, uppercase and lowercase letters count as different, and you return the one that starts first.

The judged task matches the report at about 90%.

Given a string s, return its longest palindromic substring.

A substring is a run of characters that sit next to each other in s. A palindrome reads the same from left to right and from right to left. A single character is a palindrome.

Compare characters exactly, so an uppercase letter and its lowercase letter are different. If several longest palindromic substrings exist, return the one that starts first in s.

Function

longestPalindrome(s: String) → String

Examples

Example 1

s = "abacdc"return = "aba"

The palindromes of length 3 are "aba", which starts at index 0, and "cdc", which starts at index 3. No palindrome is longer.

They tie, so return the one that starts first: "aba".

Example 2

s = "noonday"return = "noon"

"noon" reads the same both ways and has length 4. No longer substring does, so the answer is "noon".

Example 3

s = "Aa"return = "A"

"A" and "a" are different characters, so "Aa" is not a palindrome.

Both one-letter substrings are palindromes of length 1. "A" starts first, so it is the answer.

Constraints

  • 1 <= s.length <= 1000
  • s contains only English letters (uppercase and lowercase) and digits.

More Siemens problems

  • Asteroid CollisionPHONE SCREEN · Seen Nov 2026
  • Padded Vector Batch PipelinePHONE SCREEN · Seen Sep 2026
See Siemens hiring insights
Saved
class Solution {
    public String longestPalindrome(String s) {
        // Write your solution here.
    }
}
Checking account…