FastPrepNumber of Balanced Strings
Problems/Number of Balanced Strings

Number of Balanced Strings

Moveworks logoMoveworks● MediumFULLTIMEINTERNOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Corrected on 2026-07-19 because the reported first sample does not follow the written adjacent-character rule. The judged task uses lowercase English letters and returns the count modulo 1,000,000,007; it matches the visible source at about 96%.

Count the balanced strings of length n over the lowercase English alphabet.

A string is balanced when the absolute difference between the alphabet positions of every pair of adjacent characters is at most d.

Return the count modulo 1,000,000,007.

Function

numberOfBalancedStrings(n: int, d: int) → int

Examples

Example 1

n = 3d = 3return = 1134

Dynamic counting over the final letter gives 1,134 balanced lowercase strings of length 3 when adjacent letters may differ by at most 3.

Example 2

n = 2d = 2return = 124

There are 124 ordered pairs of lowercase letters whose alphabet-position difference is at most 2.

Constraints

  • 1 <= n <= 10^6
  • 0 <= d <= 25

More Moveworks interview questions

  • Underline Shortest Unique SubstringsPHONE SCREEN · Seen Oct 2026
  • Word BreakONSITE INTERVIEW · Seen Sep 2026
  • Minimum Moves to Sort ArrayOA · Seen Jul 2024
  • Find Maximum Number of StringsOA · Seen Jul 2024
  • Get Minimum Operations to Sort ArrayOA · Seen Jul 2024

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
  • Palindrome CounterGoldman Sachs · OAGoldman Sachs 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
All Moveworks interview questionsSee Moveworks hiring insights
Saved
class Solution {
    public int numberOfBalancedStrings(int n, int d) {
      // write your code here
    }

}
Checking account…