Reported July 2026
Visasliding window

Perfect Substrings

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

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

This Visa OA from July 2026 looks like a brutal substring counting problem with n up to 2*10^5. It isn't. It reduces to ten fixed-size sliding windows, one for each possible number of distinct digits. Once you see that, the O(n^2) brute force is dead and the real code is about twenty lines. The hard part is spotting the reduction under a clock. If you blank on it during the live assessment, StealthCoder sits invisibly on your screen and gives you the windowed solution so you're not staring at an empty editor. Still, read the next section first. The trick is small, and knowing it makes the rest of the assessment feel a lot less scary.

The problem

Given a digit string s and a positive integer k, count its non-empty substrings in which every digit that appears occurs exactly k times.
Return the total as a 64-bit integer. Two substrings with the same contents are counted separately when they occupy different index ranges.

Function
countPerfectSubstrings(s: String, k: int) → long

Examples
Example 1
s = "11020211"
k = 2
return = 6
The six qualifying ranges have contents 11, 110202, 102021, 0202, 020211, and the final 11. Repeated contents at different positions count separately.
Example 2
s = "2222"
k = 2
return = 3
Each length-two range contains the only present digit exactly twice, so all three length-two ranges qualify.

Constraints
1 <= s.length <= 2 * 10^5
s contains only digits from 0 through 9.
1 <= k <= s.length

Reported by candidates. Source: FastPrep

Pattern and pitfall

Every valid substring has d distinct digits, each appearing exactly k times, so its length is exactly d*k. Since there are only 10 digits, d runs from 1 to 10. For each d, slide a window of length d*k across s, keeping a count array of size 10. Track how many digits currently have count exactly k, and how many digits have count above zero. The window qualifies when both numbers equal d. That's O(10*n), easily fast enough. The common pitfall is checking only that digits with count above zero equal k, which works, but people forget to skip windows longer than n. Another is using a 32-bit int for the total, and the problem says long for a reason. Update the counters incrementally when adding and removing a digit instead of rescanning all ten slots. If the idea slips away mid-assessment, StealthCoder is your hedge for the live OA.

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 Perfect Substrings 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 Visa's OA.

Visa 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.

Perfect Substrings FAQ

What's the trick in Perfect Substrings?+

Fix the number of distinct digits d from 1 to 10. A valid substring then has length exactly d*k, so you slide one fixed-length window per d and check that every present digit has count k. That turns an O(n^2) search into roughly 10 linear passes.

How hard is this Visa OA question really?+

Medium. The code is short, but the reduction to fixed-length windows isn't obvious if you start by thinking about all substrings. Once you notice only 10 digits exist, it clicks. Most failures come from brute force timing out on n up to 2*10^5.

What complexity do I need to hit?+

Aim for O(10*n) time and O(10) extra space. Anything quadratic will time out at 200,000 characters. Make sure your counting is incremental, so each window slide updates only the digit entering and the digit leaving.

What edge cases break solutions?+

Skip any d where d*k exceeds the string length. Use a 64-bit total, since the answer can exceed int range. Remember that repeated contents at different positions count separately, so you count index ranges, not unique strings. Test the all-same-digit case like "2222" with k=2, which returns 3.

How do I prepare for this in 48 hours?+

Write the fixed-window counting solution from scratch twice. Practice tracking two counters, digits at exactly k and digits present, and updating them on add and remove. Then test on the two given examples and a single-digit string. That's enough for this pattern.

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

OA at Visa?
Invisible during screen share
Get it