Can Subsequences Share One Master Sequence?
Updated
Problem statement
You are given subsequences, a list of rows. Each row lists distinct tokens in the order they must appear.
A master sequence contains every distinct token exactly once. It must also hold the tokens of each row in that row’s order. Those tokens do not have to be next to each other.
Return true if such a master sequence exists, otherwise return false. You do not need to build the master sequence.
Function
canShareMasterSequence(subsequences: String[][]) → booleanExamples
Example 1
subsequences = [["a","b"],["a","c"]]return = trueBoth rows are subsequences of [a,b,c].
Example 2
subsequences = [["a","b"],["b","a"]]return = falseThe first row requires a before b, while the second requires the reverse.
Example 3
subsequences = [["a","c"],["a","b","c"],["b","d"]]return = trueIn [a,b,c,d], a comes before c, and a, b, c appear in that order. Also b comes before d. Every row keeps its order, so the answer is true.
Constraints
1 <= subsequences.length <= 100000.- Every row contains from
1through100000distinct tokens. - The total number of token occurrences is at most
200000. - Every token is a non-empty lowercase English string of length at most
30.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEW
- Validate and Complete Racer PrecedenceMicrosoft · ONSITE INTERVIEW
- Password From Ordering HintsRogo · ONSITE INTERVIEW
- Alien DictionaryByteDance · PHONE SCREEN · ONSITE INTERVIEW
- Alien DictionaryBloomberg LP · ONSITE INTERVIEW
- Alien DictionaryAmazon · ONSITE INTERVIEW
- Letter Order from a Sorted Word ListRippling · PHONE SCREEN · ONSITE INTERVIEW
- Alien DictionaryAirbnb · PHONE SCREEN · ONSITE INTERVIEW
- Validate CSV References and Field DependenciesStripe · PHONE SCREEN
- Alien DictionaryIntuit · OA