Reported February 2026
Salesforcedynamic programming

Replace '?' to Avoid Adjacent Duplicates

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 multiplying 26 by 25 for every '?' and calling it done. That works for "??" and breaks the moment fixed letters sit between question marks. The task is counting valid replacements so no two neighbors match, modulo 10^9 + 7, on a string up to 10^5 long. It's a clean counting DP with a small state. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and surfaces the approach while you type. Know the trick first and you won't need it.

The problem

You are given a string s consisting of lowercase English letters
('a' to 'z') and the character '?'.
Each '?' can be replaced with any lowercase English letter.
Return the total number of ways to replace all '?' such that the final
string has no two adjacent identical characters.
Since the answer may be large, return it modulo 10^9 + 7.

Function
replaceQuestionMarkToAvoidAdjacentDuplicates(s: String) → int

Examples
Example 1
s = "??"
return = 650
First character: 26 choices
Second character: 25 choices (cannot equal the previous character)
Total ways = 26 * 25 = 650

Constraints
1 <= s.length <= 10^5
s[i] is a lowercase English letter or '?'

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a DP over positions with 26 states: dp[c] is the number of valid ways for the prefix ending in letter c. For a fixed letter, only that letter's state survives, and its value is the sum of all previous states except itself. For a '?', each letter c gets total minus dp[c] from the previous step. That's 26 operations per character, so about 2.6 million total, which is fine for 10^5. The pitfalls: forgetting that an input with two equal fixed neighbors like "aa" must return 0, and letting the modulo produce negative numbers after subtraction. Add MOD before taking the remainder. Also watch the first character, where a '?' has 26 options and no restriction. StealthCoder is the hedge if the state transition won't come to you live, but the whole solution is about fifteen lines.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Replace '?' to Avoid Adjacent Duplicates 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Replace '?' to Avoid Adjacent Duplicates FAQ

What's the trick for the Salesforce replace '?' problem?+

Use a DP with 26 states, one per ending letter. For a '?', each letter's count equals the previous total minus the previous count for that same letter. For a fixed letter, keep only that state. Reduce modulo 10^9 + 7 at every step.

Why doesn't 26 * 25 ^ (k-1) work?+

It only holds for a run of consecutive question marks with no fixed letters. Fixed letters change what's forbidden for the neighbors on both sides. A '?' between 'a' and 'b' has 24 options, not 25. The DP handles this automatically.

What should the function return for input like "aa"?+

Zero. Two adjacent identical fixed letters can't be fixed by any replacement. In the DP, the second 'a' state becomes total minus dp['a'], which is zero, so the answer falls out naturally without a special case.

How hard is this really?+

Medium. The idea is simple once you see the 26-state DP. Most failures come from modulo handling with subtraction and from off-by-one at the first character. If you can write a rolling DP array, you can finish this quickly.

How do I prepare for this in 48 hours?+

Write the 26-state DP from scratch twice, once with a full table and once with two rolling arrays. Test on "?", "??", "a?a", "aa", and a 10^5 string of all '?'. Check that results never go negative after the modulo.

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