FastPrepPalindrome Counter
Problems/Palindrome Counter

Palindrome Counter

Goldman Sachs logoGoldman Sachs● MediumINTERNFULLTIMENEW GRADOA

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Special thanks: 🌷 A special thank you to a lovely, amazing fren for sharing a similar interview question, with reported date of October 17, 2026! ૮₍ ˃ ⤙ ˂ ₎ა 💜

A palindrome is a string that reads the same from the left and from the right. For example, mom and tacocat are palindromes, as are any single-character strings. Given a string, determine the number of its substrings that are palindromes.

Function

countPalindromes(s: String) → int

Complete the function countPalindromes in the editor.

countPalindromes has the following parameter:

  1. string s: the string to analyze

Returns int: an integer that represents the number of palindromic substrings in the given string

Examples

Example 1

s = "tacocat"return = 10

Palindromic substrings are ['t', 'a', 'c', 'o', 'c', 'a', 't', 'coc', 'acoca', 'tacocat']. There are 10 palindromic substrings.

Example 2

s = "aaa"return = 6

There are 6 possible substrings of s: {'a', 'a', 'a', 'aa', 'aa', 'aaa'}. All of them are palindromes, so return 6.

Example 3

s = "abccba"return = 9

There are 21 possible substrings of s, the following 9 of which are palindromes: {'a', 'a', 'b', 'b', 'c', 'c', 'cc', 'bccb', 'abccba'}.

Example 4

s = "daata"return = 7

There are 15 possible substrings of s, the following 7 of which are palindromes: {'a', 'a', 'a', 'aa', 'ata', 'd', 't'}.

Constraints

  • 1 ≤ |s| ≤ 5 x 103 each character of s, s[i] ∈ ['a'-'z'].

More Goldman Sachs interview questions

  • Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Design Hash MapOA · PHONE SCREEN · Seen Nov 2026
  • House Robber IIIPHONE SCREEN · Seen Nov 2026
  • Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
  • Maximum Ones After K OperationsOA · Seen Nov 2026
  • Longest Palindromic SubsequenceOA · Seen Oct 2026
  • Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
  • Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Longest Palindromic SubsequenceAXQ Capital · OA
  • Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
  • Longest Subsequence That Is a SubstringBooking.com · OA
  • Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
  • Longest String ChainVisa · OAVisa interview questions
  • Minimum-Cost Digit String DecodingSquadStack.ai · OA
  • Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
  • Substrings Without a Target SubsequencePhonePe · OAPhonePe interview questions
  • Minimum Edit-Distance String for Each QueryTarget · OA
  • Longest Subsequence That Is a SubstringWells Fargo · OAWells Fargo interview questions
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
class Solution {
    public int countPalindromes(String s) {
      // write your code here
    }

}
Checking account…