MEDIUMasked at 1 company

Number of Substrings With Fixed Ratio

A medium-tier problem at 56% community acceptance, tagged with Hash Table, Math, String. Reported in interviews at Intuit and 0 others.

Founder's read

You need to count substrings where the ratio of two character types stays constant. It's a math-and-counting problem that looks deceptively simple until you hit the modular arithmetic wall. Intuit has asked this, and the acceptance rate hovers around 56 percent, which means half the candidates either miss the trick or implement it wrong. The naive approach (check every substring) times out. You need a prefix-sum and hash-table insight to collapse it to linear time.

Companies asking
1
Difficulty
MEDIUM
Acceptance
56%

Companies that ask "Number of Substrings With Fixed Ratio"

If this hits your live OA

Number of Substrings With Fixed Ratio is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.

Get StealthCoder
What this means

The core trick: convert the ratio constraint into a fixed linear relationship, then use prefix sums with a hash table to count matching pairs. Instead of checking every substring against the ratio directly, you track cumulative counts of each character and recognize that two positions have the same ratio if and only if their difference vectors are proportional. This reduces the problem to hashing normalized difference pairs. The pitfall is treating it as a pure string-iteration problem when it's really a number-theory problem in disguise. If you're iterating through every substring and computing ratios on the fly, you've already lost. StealthCoder surfaces the prefix-sum decomposition and hash-table pattern in seconds if you blank on it during the live OA.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Number of Substrings With Fixed Ratio recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Substrings With Fixed Ratio interview FAQ

Is this really asked at big companies or just Intuit?+

Intuit is the only confirmed source in the data. It's not a FAANG staple, but mid-to-large fintechs and data-heavy companies ask ratio and modular-arithmetic problems constantly. The pattern matters more than the exact problem.

What's the actual trick I'm missing if I get TLE?+

You're likely checking every substring individually. You need to use prefix sums to track character counts, then hash the normalized ratio vector at each position. Two positions match a ratio constraint if their difference vectors are proportional. Hash and count, don't enumerate.

How does Hash Table relate to the solution?+

Hash tables store the normalized (or reduced) difference vectors you encounter as you build prefix sums. When you see the same vector twice, that pair of indices defines a substring satisfying the ratio. Without hashing, you'd recompute or lose track.

Why is this Medium and not Hard if half fail it?+

The acceptance rate reflects implementation mishaps and off-by-one errors more than conceptual difficulty. Once you see the prefix-sum plus hash insight, the code is straightforward. The gap is usually between 'I understand' and 'I code it right under time pressure.'

Do I need to handle edge cases around character frequencies or zero counts?+

Yes. Substrings with zero occurrences of a character complicate the ratio definition. Check the problem statement carefully for whether such substrings count or are excluded. Prefix sums make this easier to track, but you need to define your constraint upfront.

Want the actual problem statement? View "Number of Substrings With Fixed Ratio" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.