Reported February 2026
Salesforcedynamic programming

Strings With No k Consecutive Identical Characters

Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Salesforce OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Salesforce OA, reported February 2026, is brute-forcing or inclusion-exclusion on 26^n strings. n goes up to 10^5, so that dies fast. This is a counting DP in disguise. You track how long the current run of identical characters is, and you count valid strings by length. If you blank on the recurrence mid-assessment, StealthCoder runs invisibly on your screen and can hand you a working solution as a safety net. But the recurrence is short, and you can own it in ten minutes.

The problem

You are given two integers:
n: length of string
k: maximum allowed consecutive identical characters
A string is invalid if it contains k or more consecutive identical characters.
Return the total number of strings of length n formed using lowercase English letters
such that no character appears k or more times consecutively.
Return the answer modulo 10^9 + 7.

Function
stringsWithNoKConsecutiveIdenticalCharacters(n: int, k: int) → int

Examples
Example 1
n = 3
k = 2
return = 16250
Total strings of length 3 is 26^3 = 17576.
Invalid strings are those with at least one adjacent equal pair.
For this case, the valid count is 16250.

Constraints
1 <= n <= 10^5
1 <= k < n

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a DP over length where dp[i] is the number of valid strings of length i. Appending a character that differs from the previous one gives dp[i] = 25 * (dp[i-1] +... + dp[i-k+1]) for i past the first character, with the first character having 26 choices. Cleaner: let f[i] be the count ending in a run of exactly one character start. Use a sliding window sum so each step is O(1), giving O(n) total. The pitfall is the off-by-one on k. Invalid means k or more in a row, so runs are capped at k-1. Check the example: n=3, k=2 gives 26*25*25 = 16250. Also apply modulo 10^9+7 on every subtraction when you maintain the window, or you'll get negatives. If the recurrence slips under pressure, StealthCoder is the hedge on the live OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Strings With No k Consecutive Identical Characters cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Salesforce's OA.

Salesforce reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Strings With No k Consecutive Identical Characters FAQ

How hard is this Salesforce OA question really?+

Medium. The idea is simple once you see it as counting by run length, but the off-by-one on k and the modulo handling on a sliding sum trip people up. Expect a clean O(n) DP, not anything exotic.

What's the trick to solving it?+

Build strings left to right and track the current run length, capped at k-1. A new run starts with one of 25 different letters, so dp[i] sums the previous k-1 states times 25. A running window sum keeps it linear.

How do I verify my answer fast?+

Use the given example. With n=3 and k=2, no two adjacent characters can match, so the count is 26*25*25 = 16250. Also test k=1 edge ideas and a tiny n by hand before submitting.

What time complexity do I need?+

n goes up to 10^5, so you need O(n) or O(n log n). A naive sum over the last k states each step is O(nk) and can time out. Maintain a prefix sum or running window to keep it linear.

How do I prepare in 48 hours?+

Practice two or three counting DPs with run-length limits and a sliding window sum. Write the recurrence on paper, then code it with modulo on every add and subtract. Focus on boundary cases, not on memorizing code.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Salesforce.

OA at Salesforce?
Invisible during screen share
Get it