Find an A-B-Plus-C Path with Two A Predecessors
Updated
Problem statement
A directed graph has a label A, B, or C on every vertex.
Find a path whose first vertex is labeled A, followed by one or more B vertices, and whose final vertex is labeled C. Additionally, the first B vertex on the path must have incoming edges from at least two distinct A-labeled vertices.
Return a valid path with the fewest vertices. If paths tie in length, compare their vertex indices from the start. Choose the path with the smaller index at the first difference. Return an empty array when none exists.
Function
findABPlusCPath(labels: String, edges: int[][]) → int[]Examples
Example 1
labels = "AABBC"edges = [[0,2],[1,2],[2,3],[3,4]]return = [0,2,3,4]B vertex 2 has A predecessors 0 and 1; the lexicographically smaller start A is 0.
Example 2
labels = "ABBC"edges = [[0,1],[1,2],[2,3]]return = []The first B has only one A predecessor.
Example 3
labels = "AABC"edges = [[0,2],[1,2],[2,3]]return = [0,2,3]One B between A and C satisfies B-plus.
Constraints
1 <= labels.length <= 500.0 <= edges.length <= 10000.- Labels contain only A, B, and C; edges contain valid distinct vertices.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Bus RoutesUiPath · PHONE SCREEN
- Minimum Bus Routes to a DestinationPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Find All People with the SecretAmazon · PHONE SCREEN
- Grid Robot Route with Charging PrioritiesUber · PHONE SCREEN · ONSITE INTERVIEWUber interview questions
- Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Shortest Word Transformation SequenceReddit · PHONE SCREENReddit interview questions
- Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
- Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Bus RoutesBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Shortest Path Around Moving ParadesZip · PHONE SCREENZip interview questions