Halfway Course in a Single Track
Updated
Problem statement
Students in a program take courses along a track. You get the track as pairs, where [source, destination] means destination is taken right after source. The pairs come in any order, and together they form one straight track: every course except the first has exactly one course right before it, and every course except the last has exactly one course right after it.
Return the course a student is taking when they are halfway through the track. If the track has L courses numbered from 0, this is the course at position (L - 1) / 2, rounded down. So when L is even, return the first of the two middle courses.
Function
halfwayCourse(pairs: String[][]) → StringExamples
Example 1
pairs = [["Logic","Networking"],["Compilers","Logic"],["Algorithms","Compilers"],["Data Structures","Algorithms"],["Intro to Computer Science","Data Structures"]]return = "Algorithms"- The track is Intro to Computer Science → Data Structures → Algorithms → Compilers → Logic → Networking, which has 6 courses.
- With an even count there are two middle courses, Algorithms (position 2) and Compilers (position 3). Return the first one: Algorithms.
Example 2
pairs = [["Networking","Graphics"],["Logic","Networking"],["Compilers","Logic"],["Algorithms","Compilers"],["Data Structures","Algorithms"],["Intro to Computer Science","Data Structures"]]return = "Compilers"Graphics now follows Networking, so the track has 7 courses. The single middle course is at position 3: Compilers.
Constraints
1 <= pairs.length <= 1000- Each pair is
[source, destination]with two different course names. - A course name has 1 to 30 characters: letters, digits, spaces and underscores, with no space at the start or end and no two spaces in a row.
- The pairs link all the courses into one track with no branches, no cycles and no repeated pair, so the track has
pairs.length + 1courses.
More Crunchyroll Phone Screen questions
Similar questions at other companies
- Find Root IDs from Parent ReferencesZip · PHONE SCREENZip interview questions
- Resolve Key-Value ChainsIndeed · PHONE SCREENIndeed interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Shortest Word Transformation SequenceReddit · PHONE SCREENReddit interview questions
- Evaluate Live Spreadsheet ExpressionsStrala · PHONE SCREEN