PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency with string algorithms and algorithmic problem-solving, focusing on palindrome detection and counting contiguous substrings. It is commonly asked to assess implementation skills and the ability to reason about time and space complexity within the Coding & Algorithms domain, emphasizing practical application rather than purely conceptual understanding.

  • medium
  • Reevo
  • Coding & Algorithms
  • Software Engineer

Count all palindromic substrings

Company: Reevo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

Given a string `s`, count how many substrings of `s` are palindromes. - A substring is a contiguous block of characters. - A palindrome reads the same forward and backward. - Count **all** palindromic substrings (including duplicates by position). Return the total count. Constraints (typical): `1 ≤ |s| ≤ 2000` (so an `O(n^2)` approach is acceptable).

Quick Answer: This question evaluates proficiency with string algorithms and algorithmic problem-solving, focusing on palindrome detection and counting contiguous substrings. It is commonly asked to assess implementation skills and the ability to reason about time and space complexity within the Coding & Algorithms domain, emphasizing practical application rather than purely conceptual understanding.

Given a string `s`, count how many substrings of `s` are palindromes. - A **substring** is a contiguous block of characters. - A **palindrome** reads the same forward and backward. - Count **all** palindromic substrings, counting duplicates that occur at different positions separately. Return the total count. **Example 1:** ``` Input: s = "abc" Output: 3 Explanation: The palindromic substrings are "a", "b", "c". ``` **Example 2:** ``` Input: s = "aaa" Output: 6 Explanation: "a", "a", "a", "aa", "aa", "aaa". ```

Constraints

  • 1 <= |s| <= 2000
  • s consists of lowercase (and/or any printable) characters
  • An O(n^2) approach is acceptable

Examples

Input: "abc"

Expected Output: 3

Explanation: Each single character is a palindrome: "a", "b", "c". No longer substring is a palindrome.

Input: "aaa"

Expected Output: 6

Explanation: Three single chars ("a" x3), two "aa", and one "aaa" = 6.

Input: "a"

Expected Output: 1

Explanation: A single character is itself a palindrome.

Input: "aba"

Expected Output: 4

Explanation: "a", "b", "a", and "aba" = 4.

Input: "abba"

Expected Output: 6

Explanation: "a", "b", "b", "a", "bb", and "abba" = 6.

Input: "racecar"

Expected Output: 10

Explanation: 7 single chars, plus "cec", "aceca", "racecar" = 10.

Input: "zzzz"

Expected Output: 10

Explanation: 4 + 3 + 2 + 1 = 10 palindromic substrings of a run of identical characters.

Hints

  1. Every palindrome has a center. For a string of length n there are 2n-1 possible centers: n single-character centers (odd-length palindromes) and n-1 gaps between characters (even-length palindromes).
  2. From each center, expand outward with two pointers while the characters on both sides match; every successful expansion yields one more palindromic substring.
  3. This center-expansion runs in O(n^2) time and O(1) extra space, which fits the |s| <= 2000 constraint comfortably.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Output lexicographically largest DFS traversal - Reevo (medium)
  • Maximize weighted sum with disjoint adjacent swaps - Reevo (medium)
  • Check strings against dictionary efficiently - Reevo (Medium)