FastPrepGood Strings
Problems/Good Strings

Good Strings

MathWorks logoMathWorks● MediumOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given two integers n and d. Returns the numbers of Good Strings having length n and absolute difference between the adjacent characters 'a' to 'z' is not more than d.

a and z have absolute difference 25. b and c have absolute difference 1.

Function

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

GoodStrings has the following parameters:

  1. n: The size of the string.
  2. d: The difference.

Returns int number of Good Strings mod 10^9 + 7.

Examples

Example 1

n = 2d = 2return = 124

There are 124 Good Strings of length 2 where the absolute difference between adjacent characters is not more than 2.

Example 2

n = 3d = 5return = 2596

There are 2596 Good Strings of length 3 where the absolute difference between adjacent characters is not more than 5.

Constraints

  • n ≤ 105
  • d ≤ 25

More MathWorks interview questions

  • Earliest Time to Schedule a MeetingOA · Seen Nov 2026
  • Minimum Cost to Serve Requests on a RingOA · Seen Nov 2026
  • Minimum Tree Diameter After Removing K LeavesOA · Seen Nov 2026
  • Cities on Shortest Delivery RoutesOA · Seen Nov 2026
  • Repeated Mirror XOROA · Seen Nov 2026
  • Beautiful ArrangementONSITE INTERVIEW · Seen Jul 2026
  • Find Minimum Cost to Remove Array ElementsONSITE INTERVIEW · Seen Jul 2026
  • Group Shifted StringsONSITE INTERVIEW · Seen Jul 2026
  • Longest Valid ParenthesesONSITE INTERVIEW · Seen Jul 2026
  • Balancing TeamsOA · Seen Nov 2025

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

MathWorks interview experiences

  • MathWorks SWE Intern Phone Screens: 1 Experience
  • MathWorks Software Engineer in Test OA and HireVue: 1 Experience
All MathWorks interview questionsSee MathWorks hiring insights
Saved
class Solution {
    public int goodStrings(int n, int d) {
      // write your code here
    }

}
Checking account…