Middle Course of a Course Track
Updated
Problem statement
A course track is a sequence of courses where each course must be taken right before the next one. You are given a track as a list of pairs [prerequisite, course], meaning prerequisite comes immediately before course. The pairs can come in any order.
Together, the pairs link every course into one straight line: exactly one course has no prerequisite, every other course has exactly one, and no course comes immediately before two different courses.
Return the course in the middle of the track. If the track has k courses, this is the course at position (k - 1) / 2, rounded down and counted from 0 at the first course. So when k is even, return the earlier of the two middle courses.
Course names are case-sensitive: "Art" and "art" are different courses.
Function
middleCourse(pairs: String[][]) → StringExamples
Example 1
pairs = [["Statistics","Machine Learning"],["Calculus","Statistics"],["Machine Learning","Deep Learning"]]return = "Statistics"The track is Calculus, Statistics, Machine Learning, Deep Learning. It has 4 courses, so the two middle courses are Statistics and Machine Learning; return the earlier one.
Example 2
pairs = [["Intro","Data Structures"],["Algorithms","Compilers"],["Data Structures","Algorithms"],["Compilers","Thesis"]]return = "Algorithms"The track is Intro, Data Structures, Algorithms, Compilers, Thesis. With 5 courses, the middle one is at position 2.
Example 3
pairs = [["Spanish 1","Spanish 2"]]return = "Spanish 1"Two courses: the earlier of the two middle courses is the first one.
Example 4
pairs = [["Art","ART"],["art","Art"]]return = "Art"Names are case-sensitive, so these are three different courses. The track is art, Art, ART, and the middle course is Art.
Constraints
1 <= pairs.length <= 104pairs[i].length == 2- Each course name has
1to20characters: English letters, digits and spaces, with no space at the start or end and no two spaces in a row. - The pairs form one straight track of
pairs.length + 1different courses, with no cycle and no branch.
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE 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
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- 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