Most-Read Page Across Every Valid Storyline
Updated
Problem statement
You are reading a branching story book whose pages are numbered from 1 through 50. Every storyline begins on page 1.
On an ordinary page, reading continues to the next numbered page. A row [page, first, second] in choices makes page a choice page whose two options jump to first and second. A page in endings finishes the current storyline immediately.
Within one storyline, each option of a choice page can be used at most once. Revisiting a choice page after both options have already been used makes that branch invalid. Advancing past page 50 also makes a branch invalid.
Consider every valid storyline that reaches an ending. Count every visit to every page across all of those storylines. Return [page, count] for the page with the greatest total count. If several pages tie, return the smallest page number. If no valid storyline reaches an ending, return [-1].
Function
mostReadStoryPage(endings: int[], choices: int[][]) → long[]Examples
Example 1
endings = [5,10]choices = [[3,7,9],[9,10,8]]return = [9,6]There are four valid ending-reaching storylines. Pages 1, 2, 3, 8, and 10 are each read four times, while page 9 is read six times. Therefore the result is [9, 6].
Example 2
endings = [5]choices = [[1,1,1]]return = [-1]Both options on page 1 return to page 1. After each option has been used once, the branch is invalid and never reaches page 5. No valid storyline exists.
Constraints
1 <= endings.lengthand every ending is a unique page from1through50.0 <= choices.length <= 12.- Every choice row has exactly three page numbers from
1through50. - Choice-page numbers are unique.
- Counts fit in a signed 64-bit integer.
More Duolingo interview questions
- Bouncing Square Reaches a Screen CornerOA · Seen Dec 2024
- Shortest Route Through a Traced MazeOA · Seen Sep 2022
- Rank Common Translation MistakesOA · Seen Sep 2022
- Count Distinct Word MeaningsOA · Seen Sep 2021
- Count Legal Outfit CombinationsOA · Seen Sep 2021
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Attraction Route Without Reusing TrailsWhatNot · PHONE SCREENWhatNot interview questions
- Enumerate Directed Paths and CyclesByteDance · PHONE SCREENByteDance interview questions
- Count Prefix Paths in a Character GraphGoogle · PHONE SCREENGoogle interview questions
- All Paths From Source to TargetBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Park Train Cars by PreferenceMercor · ONSITE INTERVIEWMercor interview questions
- Find Trapped ZonesServiceNow · PHONE SCREENServiceNow interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Sum Dynamic Weighted Tree DistancesJuspay · OA
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Course Order and CycleAmazon · PHONE SCREEN · ONSITE INTERVIEW