Count Prime Strings

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 in June 2026, is treating it like a prime-checking problem when it's really a counting DP. Count Prime Strings gives you a digit string up to 10^5 long and asks how many ways you can cut it into primes between 2 and 10^6, with no leading zeros, modulo 10^9 + 7. Brute-force splitting dies instantly. If you blank on the setup during the live assessment, StealthCoder runs invisibly as a safety net and hands you the structure. Know the shape before you sit down.

The problem

Given a string s representing a non-negative decimal integer, count the number of ways to split it into one or more prime numbers.
A valid split must follow all of these rules:
The digits remain in their original order, and every digit is used exactly once.
Each piece is interpreted as a decimal integer between 2 and 10^6, inclusive.
No piece may contain a leading zero.
Return the number of valid splits modulo 10^9 + 7.

Function
countPrimeStrings(s: String) → int

Examples
Example 1
s = "11375"
return = 3
The string can be split into primes in three ways: [11, 37, 5], [11, 3, 7, 5], and [113, 7, 5].

Constraints
1 <= s.length <= 10^5
s contains only decimal digits.
s[0] != '0'

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: dp[i] is the number of ways to split the first i characters. For each i, look back at most 7 characters, since a piece can't exceed 10^6 (that's 7 digits only for exactly 1000000, which isn't prime anyway). For each start j, skip if s[j] is '0', parse the piece, and if it's prime, add dp[j] to dp[i]. Precompute primes up to 10^6 with a sieve once. Pitfalls: forgetting the leading zero rule, forgetting to mod on every addition, treating 1 as prime, and re-running primality checks per piece instead of a sieve lookup. Complexity is about O(7n) after the sieve, which is fine for n = 10^5. If the sieve or the window logic slips under pressure, StealthCoder is the hedge on the live OA, but the DP itself is only a dozen lines.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Count Prime Strings 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

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 for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Prime Strings FAQ

What's the trick in Count Prime Strings?+

Treat it as a prefix DP. dp[i] counts valid splits of the first i digits. Each state only looks back up to 7 characters because pieces are capped at 10^6. Use a sieve for O(1) primality lookups and add dp[j] when s[j..i) is prime with no leading zero.

How hard is this one really?+

Medium. The idea is a standard partition DP, like decoding ways. The difficulty is the combination: sieve, bounded lookback window, leading zero rule, and modulo. Each part is easy alone. People lose points by missing one of them, not by lacking the core idea.

Why can't I just check every substring for primality?+

With n up to 10^5, trying all substrings is quadratic and parsing huge numbers is wasteful. The 10^6 cap means you only need windows of length 1 to 7. Combined with a precomputed sieve, each check is a single array lookup.

What edge cases break most solutions?+

Pieces starting with '0' must be rejected, even like '07'. The number 1 isn't prime, and 0 isn't allowed. Forgetting the modulo on additions causes overflow in some languages. Also check the base case dp[0] = 1, or every answer comes out zero.

How do I prepare in 48 hours?+

Write the sieve of Eratosthenes up to 10^6 from memory. Then solve a partition-counting DP like decode ways until the dp[i] from dp[j] loop is automatic. Finally, test this problem on the sample '11375' and confirm you get 3 before you trust your 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