FastPrepCount Palindromic Substrings of Length at Least Two
Problems/Count Palindromic Substrings of Length at Least Two

Count Palindromic Substrings of Length at Least Two

Morgan Stanley logoMorgan Stanley● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley online assessment for a full-time role. The report asks for a count of palindromic substrings. The original author links a reference that counts substrings of length at least 2. Its abaab example returns 3. We keep that rule. We chose lowercase ASCII input, the length limits, what happens for empty input, and the function interface.

Our version is about 95% the same as the reported question.

Given a string s, count its palindromic substring occurrences whose length is at least 2. A substring is a contiguous interval, and it is palindromic if its characters read the same forward and backward.

Count different index intervals separately, even when they contain the same text. Single-character substrings do not count. For this exercise, s contains only lowercase English letters; an empty string returns 0. Leave s unchanged.

Function

countPalindromicSubstrings(s: String) → int

Examples

Example 1

s = "abaab"return = 3

The qualifying intervals contain aba at indices 0..2,aa at 2..3,and baab at 1..4. Their lengths are 3,2,4; no singleton is included, so return 3.

Example 2

s = "aaa"return = 3

There are two length-2 occurrences aa, at 0..1 and 1..2, plus one length-3 occurrence aaa. They count separately, so return 3 rather than the number of distinct texts.

Constraints

  • For this exercise, assume 0 <= s.length <= 500.
  • Every character is a lowercase English letter a through z.

More Morgan Stanley Online Assessment questions

  • Three-Resource 0/1 KnapsackOA · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
  • Count Shipment SchedulesOA · Seen Apr 2026
  • Delete and EarnOA · Seen Apr 2026
  • Interleave Array HalvesOA · Seen Apr 2026
  • Get Patch SequenceOA · Seen Feb 2025
  • String Subsequences 🐳OA · Seen Mar 2024
  • Binary Tree Level OrderOA · Seen Feb 2024
  • Merge IntervalsOA · Seen Feb 2024

Similar questions at other companies

  • Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
  • Longest Subsequence That Is a SubstringBooking.com · OA
  • Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OA
  • Palindrome CounterGoldman Sachs · OA
  • Longest String ChainVisa · OA
  • Minimum-Cost Digit String DecodingSquadStack.ai · OA
  • Maximum Redundancy After One Segment FlipBloomberg LP · OA
  • Substrings Without a Target SubsequencePhonePe · OA
  • Minimum Edit-Distance String for Each QueryTarget · OA
  • Longest Subsequence That Is a SubstringWells Fargo · OA
See Morgan Stanley hiring insights
Saved
class Solution {
    public int countPalindromicSubstrings(String s) {
        // Write your code here.
    }

}
Checking account…