Choose Your Own Ending
Updated
Problem statement
A branching story has pages numbered from 0 to n - 1. The two integers in nextPages[p] describe page p:
[-1,-1]means that pagepis a terminal ending.- Otherwise, the first integer is the next page for choice
0, and the second is the next page for choice1.
Start at page 0 and consume the supplied choices in order. Stop as soon as a terminal page is reached and return its page number; ignore any choices left over. If all choices are consumed while the current page is not terminal, return -1.
Pages may form cycles and may be visited repeatedly. The finite choice sequence limits how many transitions are performed; do not search for an ending that the supplied choices do not reach.
Function
chooseEnding(nextPages: int[][], choices: int[]) → intExamples
Example 1
nextPages = [[1,2],[-1,-1],[1,3],[-1,-1]]choices = [1,1]return = 3Starting at page 0, the two right choices move to page 2 and then terminal page 3.
Example 2
nextPages = [[1,2],[-1,-1],[1,3],[-1,-1]]choices = [1]return = -1The only choice reaches page 2, which is not terminal. No choices remain.
Example 3
nextPages = [[-1,-1]]choices = [0,1]return = 0The starting page is already an ending, so the extra choices are ignored.
Constraints
1 <= nextPages.length <= 50.- Every row has exactly two values. A row is either
[-1,-1]or contains two valid page numbers from0tonextPages.length - 1. 0 <= choices.length <= 200, and every choice is0or1.
More WhatNot Phone Screen questions
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Lazy Filter over a GeneratorSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Array-Backed Set Without Library CollectionsOde with Anthropic · PHONE SCREEN
- Paginate Retained Conversation HistorySierra · PHONE SCREENSierra interview questions
- Validate a Chess Bishop, Rook, or Queen MoveGlean · PHONE SCREENGlean interview questions
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Bounded IPv4 FilterAmazon · PHONE SCREEN