Reported July 2025
Salesforceprefix sum

Integrity Score

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

Salesforce reported this one in July 2025, and the input size is the whole story. The string can hit 10^5 characters, so checking every substring means about 5 billion pairs. That's dead on arrival. The problem is called Integrity Score: map each letter to a value from 1 to 9, then count substrings whose value sum is divisible by their length. It looks like a string problem. It's really a prefix sum problem with a small-value trick. If you blank during the live OA, StealthCoder runs invisibly on your desktop and gives you the approach in real time.

The problem

A data packet is represented as a string dataPacket of lowercase English letters. Each character has the following value:
a and b have value 1.
c, d, and e have value 2.
f, g, and h have value 3.
i, j, and k have value 4.
l, m, and n have value 5.
o, p, and q have value 6.
r, s, and t have value 7.
u, v, and w have value 8.
x, y, and z have value 9.
The integrity score is the number of substrings whose character-value sum is divisible by the substring length. Return the integrity score of dataPacket as a long.

Function
integrityScore(dataPacket: String) → long

Examples
Example 1
dataPacket = "cat"
return = 4
The character values are [2, 1, 7]. The valid substrings are "c", "a", "t", and "at". The substring "at" has sum 8 and length 2, so its sum is divisible by its length.

Constraints
1 <= dataPacket.length <= 10^5
dataPacket consists of lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Rewrite the condition. Sum divisible by length means sum/length is an integer k, so the average value is an integer. Values only run 1 to 9, so k is between 1 and 9. For each k, subtract k from every character value and build prefix sums. A substring has sum = k * length exactly when its shifted sum is zero, meaning two equal prefix sums. Count equal pairs with a hash map or an array offset. That's 9 passes of O(n), so about 9n total. The pitfall is brute forcing with prefix sums, which is still O(n^2) and times out. Another trap is returning an int. The answer can reach roughly n^2/2, so use a long. Single characters always count, since any value is divisible by 1. If the trick slips away mid-assessment, StealthCoder is the hedge that surfaces the k loop and the prefix-equality count.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Integrity Score 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Integrity Score FAQ

What's the trick in Integrity Score?+

Divisible by length means the average is an integer, and values only go from 1 to 9. So loop k from 1 to 9, subtract k from each value, and count pairs of equal prefix sums. Each equal pair is one valid substring.

Why does brute force fail here?+

The string can be 10^5 long, so there are about 5 billion substrings. Even with prefix sums for O(1) sums, you still check every pair. That's too slow. You need a counting approach that runs in linear time per k.

What time complexity should I aim for?+

O(9n), which is just O(n). Each of the nine k values does one pass with a hash map or offset array of prefix sums. Memory is O(n) for the counts. Anything quadratic will fail on the largest inputs.

Do I need a long for the answer?+

Yes. The problem says return a long. With n up to 10^5, the count can be in the billions, which overflows a 32-bit int. Use a 64-bit type for the counter and for the pair accumulation.

How do I prep for this in 48 hours?+

Practice the pattern of turning a divisibility or average condition into a zero-sum condition with prefix sums and a frequency map. Do a few subarray-sum-equals-k style problems. Then write this one once from scratch, including the k loop and the long return.

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