Count Distinct Substrings of a Short DNA Read
Last updated: November 29, 2026
Quick Overview
Given a string of length up to a few thousand, count how many distinct non-empty substrings it has.
0
0
0 solved
Given a string of length up to a few thousand, count how many distinct non-empty substrings it has.
Asked in Google technical screens, sometimes framed around genomics work at Google Health or Verily-style data. The interviewer checks that you see each distinct substring as a unique trie node when inserting all suffixes, and that you can discuss suffix automata for larger inputs.
What the Interviewer Expects
- Show why a hash set of all substrings costs cubic time due to slicing
- Insert every suffix into a trie and count new nodes
- Argue that each trie node corresponds to exactly one distinct substring
- Give O(n^2) time and memory
- Mention suffix array or suffix automaton for long strings
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you handle a string of length 10^6?
- How would you count distinct substrings of exactly length k?
- How would you find the longest substring that appears at least twice?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: string s over any alphabet, length n up to about 3000. Output: the number of distinct non-empty substrings....
High-Level Design
Every substring is a prefix of some suffix. Insert all n suffixes into a trie. Each node (other than the root) represents the string spelled from the root to it, and two different nodes spell differ...