PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Quora

Count distinct palindromic subsequences

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in string algorithms and combinatorial counting, focusing on recognition and enumeration of palindromic subsequences and techniques for deduplicating distinct results.

  • medium
  • Quora
  • Coding & Algorithms
  • Software Engineer

Count distinct palindromic subsequences

Company: Quora

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a string `s` consisting of lowercase English letters, compute how many **distinct palindromic subsequence strings** of lengths **2**, **3**, and **4** can be formed from `s`. A subsequence is formed by deleting zero or more characters without changing the relative order of the remaining characters. Count palindromes by their resulting string value, **not** by the number of index combinations. For example, if the same palindrome can be formed in multiple ways, it should be counted only once. Return three values: 1. the number of distinct palindromic subsequences of length 2, 2. the number of distinct palindromic subsequences of length 3, 3. the number of distinct palindromic subsequences of length 4. Examples of valid palindromes include `aa` for length 2, `aba` for length 3, and `abba` or `aaaa` for length 4.

Quick Answer: This question evaluates proficiency in string algorithms and combinatorial counting, focusing on recognition and enumeration of palindromic subsequences and techniques for deduplicating distinct results.

Quora logo
Quora
Mar 19, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
5
0

Given a string s consisting of lowercase English letters, compute how many distinct palindromic subsequence strings of lengths 2, 3, and 4 can be formed from s.

A subsequence is formed by deleting zero or more characters without changing the relative order of the remaining characters.

Count palindromes by their resulting string value, not by the number of index combinations. For example, if the same palindrome can be formed in multiple ways, it should be counted only once.

Return three values:

  1. the number of distinct palindromic subsequences of length 2,
  2. the number of distinct palindromic subsequences of length 3,
  3. the number of distinct palindromic subsequences of length 4.

Examples of valid palindromes include aa for length 2, aba for length 3, and abba or aaaa for length 4.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Quora•More Software Engineer•Quora Software Engineer•Quora Coding & Algorithms•Software Engineer Coding & Algorithms
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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.