FastPrepCount Palindromes of a Given Length
Problems/Count Palindromes of a Given Length

Count Palindromes of a Given Length

Jane Street logoJane Street● EasyNEW GRADFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in six Jane Street interview reports from 2023 to 2026, for trading desk operations roles, mostly in phone screens and math or brainteaser calls. One report asks how many 3-letter and 5-letter palindromes the 26-letter alphabet has, worked out without a calculator, and another asks only about 5-letter palindromes. The other four only name the topic: one mentions palindrome questions in general, one counting palindromes with properties it does not give, one a combinatorics question about palindromes, and one a riddle about palindromes and permutations.

In the interview it was a mental-math question. We turned it into a function that takes any word length and alphabet size. The large limits, the answer modulo 10^9 + 7, and every example except the first two are our own.

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

A palindrome is a word that reads the same from left to right and from right to left.

You are given an alphabet of alphabetSize different letters. Count how many words of exactly length letters, made from this alphabet, are palindromes. Letters may repeat, and the words do not have to be real words.

The count can be very large, so return it modulo 109 + 7.

Function

countPalindromes(length: int, alphabetSize: int) → int

Examples

Example 1

length = 3alphabetSize = 26return = 676

A 3-letter palindrome looks like aba: the first letter decides the last one. You pick the first letter in 26 ways and the middle letter in 26 ways, so there are 26 * 26 = 676 palindromes.

Example 2

length = 5alphabetSize = 26return = 17576

A 5-letter palindrome is decided by its first three letters, so there are 26 * 26 * 26 = 17576 palindromes.

Example 3

length = 4alphabetSize = 2return = 4

With letters a and b, the first two letters decide the word: aaaa, abba, baab and bbbb. That is 2 * 2 = 4.

Example 4

length = 1alphabetSize = 7return = 7

Every single letter reads the same both ways, so all 7 one-letter words count.

Example 5

length = 1000000000alphabetSize = 1000000000return = 206997086

The first 500000000 letters decide the word, so the count is 1000000000500000000. Taken modulo 109 + 7, that is 206997086.

Constraints

  • 1 <= length <= 109
  • 1 <= alphabetSize <= 109

More Jane Street Phone Screen questions

  • Maximum-Scoring Consecutive Tree LevelsPHONE SCREEN · Seen Nov 2026
  • Tree Selection and VisibilityPHONE SCREEN · Seen Nov 2026
  • Load/Store Arithmetic InterpreterPHONE SCREEN · Seen Oct 2026
  • Instrument Stack Language JumpsPHONE SCREEN · Seen Oct 2026
  • Run a Stack Language InterpreterPHONE SCREEN · Seen Oct 2026
  • Positive-Integer Expression EvaluationPHONE SCREEN · Seen Oct 2026
  • Add Two Integers Given as StringsPHONE SCREEN · Seen Sep 2026
  • Angle Between Clock HandsPHONE SCREEN · Seen Jul 2026
  • Route Length From Two CyclistsPHONE SCREEN · Seen Jul 2026
  • Sort TradesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026

Similar questions at other companies

  • Biased Coin: Probability of Exactly h HeadsAkuna Capital · PHONE SCREENAkuna Capital interview questions
  • Revolver: Will the Next Shot Fire?Tower Research Capital · PHONE SCREEN
  • Probability of Consecutive HeadsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
  • Expected Fake-Name CollisionsMercor · PHONE SCREENMercor interview questions
  • One Number Is the Average of the Other TwoSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
  • Count Routes Through a City GridIMC Trading · PHONE SCREENIMC Trading interview questions
  • Extra Coin Win ProbabilityMicrosoft · PHONE SCREENMicrosoft interview questions
  • Count Clean Attendance LogsRippling · PHONE SCREENRippling interview questions
  • Count Array DerangementsInMobi · PHONE SCREENInMobi interview questions
  • Next Pascal Row CoefficientIntuit · OAIntuit interview questions

Interview reports that mention this question

  • Jane Street TDO UK and Hong Kong: 22 Experiences (Part 10 of 10)
  • Jane Street TDOE US Final Round: 11 Experiences (Part 5 of 10)
  • Jane Street TDOE UK Coding Round: 15 Experiences (Part 3 of 10)
  • Jane Street TDOE UK Final Round: 14 Experiences (Part 2 of 10)
  • Jane Street TDOE Interviews: 165 Experiences (Part 1 of 10)
  • Jane Street TDOE New Grad Interview Questions: 11 Experiences
All Jane Street interview questionsSee Jane Street hiring insights
Saved
class Solution {
    public int countPalindromes(int length, int alphabetSize) {
      // Write your code here.
    }
}
Checking account…