MEDIUMasked at 4 companies

Number of Distinct Substrings in a String

A medium-tier problem at 65% community acceptance, tagged with String, Trie, Rolling Hash. Reported in interviews at Dunzo and 3 others.

Founder's read

You need to count every distinct substring a string contains. Uber, Intuit, ServiceNow, and Dunzo have all asked this. The naive approach (generate all substrings, dump them in a set) works on small inputs but tanks on larger ones. There's no time limit trick here. The real catch is picking the right data structure. A trie works. Rolling hash works faster. Most candidates blank on the hash variant and burn time rebuilding. If this hits your live assessment and you freeze on optimization, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
4
Difficulty
MEDIUM
Acceptance
65%

Companies that ask "Number of Distinct Substrings in a String"

If this hits your live OA

Number of Distinct Substrings in a String 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 Amazon engineer who used it to pass JPMorgan's OA and system design loop.

Get StealthCoder
What this means

The problem feels simple: for a string of length n, there are n*(n+1)/2 possible substrings. Throw them in a hash set and return the count. That passes maybe 70 percent of test cases. The trap is that comparing strings in a set is O(n) per operation on longer inputs, which blows up time complexity. Rolling hash (a Trie topic in disguise) hashes each substring in O(1) amortized time and stores the hashes instead. The other gotcha is off-by-one errors in substring indexing. Most people either include the empty string when they shouldn't, or miss single characters. Code carefully around loop bounds. Suffix arrays are overkill for this problem but mentioned in the topic list. If you nail rolling hash early, StealthCoder becomes unnecessary backup. If you're stuck on optimization during the assessment, you have a safety net.

Pattern tags

The honest play

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

Number of Distinct Substrings in a String 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 Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Distinct Substrings in a String interview FAQ

Is this problem really asked at Uber and Intuit?+

Yes. Four companies in the input data have asked it: Uber, Intuit, ServiceNow, and Dunzo. The 65 percent acceptance rate suggests it's not trivial. Most people solve it brute-force and pass partial test cases. Optimization separates the accepted from the rejected.

What's the actual trick to avoid timeout?+

Rolling hash. Hash each substring once, store hashes in a set instead of strings. Computation moves from O(n^3) or O(n^2 log n) to O(n^2). The trie topic hints at character-by-character insertion. If you implement trie naively, you're still doing string comparisons. Hash is faster in practice.

Do I need to implement a suffix array for this?+

No. Suffix arrays appear in the topic list but are overkill. They're useful for other substring problems like finding the longest repeated substring. Rolling hash or a well-tuned trie gets the job done. Save suffix arrays for when the problem explicitly demands them.

What mistake do most people make?+

Off-by-one errors in the substring loop. Accidentally including the empty string or skipping single characters. Also, implementing rolling hash without a good prime modulus, which causes hash collisions on edge cases. Test your indexing on a 3-character string before submitting.

How does this relate to hash functions?+

It's the core application. You're building a rolling polynomial hash to avoid string object overhead. The hash function topic isn't just theory here. Choosing the right base and modulus directly impacts whether you collide or pass. This is why hash function fundamentals matter in interviews.

Want the actual problem statement? View "Number of Distinct Substrings in a String" 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.