Middle Course in One Prerequisite Chain
Updated
Problem statement
Each row of pairs is [prerequisite,nextCourse], meaning prerequisite comes right before nextCourse. Return the middle course in this order of prerequisites.
The pairs link all the courses into one straight chain. The chain never branches, and there is no second chain. You can assume there are no cycles, a repeated pair changes nothing, and the only courses are the ones that appear in pairs.
The middle course is the one at position floor((numberOfCourses-1)/2), counting from zero at the first course. Here numberOfCourses is the number of courses in the chain. When the chain has an even number of courses, pick the earlier of the two middle courses.
If pairs is empty, there are no courses, so return an empty string. A single course on its own cannot be written as pairs, so every non-empty input has at least two courses.
Function
middleCourse(pairs: String[][]) → StringExamples
Example 1
pairs = [["M1","M2"],["M2","M3"],["M3","M4"]]return = "M2"The pairs form the chain M1, M2, M3, M4. With four courses, there are two middle courses, M2 and M3. The earlier one is picked, so the answer is M2.
Example 2
pairs = [["C","D"],["A","B"],["D","E"],["B","C"]]return = "C"The pairs are shuffled, but they link up as A, B, C, D, E. The middle of these five courses is C, so the answer is C.
Constraints
0 <= pairs.length <= 250, and each row has exactly two strings.- Once repeated pairs are ignored, a non-empty input forms exactly one chain with no cycles and no branches. It is never split into several chains, and no course stands alone.
- Course names are distinct, case-sensitive, printable ASCII strings of length
1through10.
More Robinhood interview questions
- Top Ten Most Frequent Words in a BookPHONE SCREEN · Seen Oct 2026
- Fractional Share Inventory FlatteningPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Unique Transitive Dependent CountsPHONE SCREEN · Seen Sep 2026
- Service Dependency Load FactorsPHONE SCREEN · Seen Sep 2026
- Word Frequency in a Large TextPHONE SCREEN · Seen Jul 2026
- Earliest One-Hour Badge Alert TimesPHONE SCREEN · Seen Sep 2021
- Courses Shared by Every Student PairPHONE SCREEN · Seen Sep 2019
- Friendship and Balance TrackerONSITE INTERVIEW · Seen Sep 2026
- Transitive Employee Referral CountsONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Remove All Repeated Values from a Linked ListArista Networks · PHONE SCREENArista Networks interview questions
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Check a Linked List for a CycleZillow · PHONE SCREENZillow interview questions
- Where Two Linked Lists MeetNutanix · PHONE SCREENNutanix interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheAthenahealth · PHONE SCREEN
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Balanced Session ManagerPalantir · PHONE SCREENPalantir interview questions
- LRU CacheGimlet Labs · PHONE SCREEN