Shared Courses for Each Pair of Students
Updated
Problem statement
A school stores its course enrollments as rows [studentId, course]. Each row says that one student takes one course.
For every pair of different students who appear in enrollments, find the courses that both of them take. Return one row per pair, written as [firstId, secondId, course1, course2, ...]. A pair with no course in common still gets a row; it holds only the two IDs.
Order the answer like this, so that it is unique:
- Student IDs are compared as numbers, so
"9"comes before"10". - In each row, the smaller ID comes first.
- The shared courses in a row are sorted by character code, as in standard string sorting. For example, uppercase letters come before lowercase letters.
- Rows are sorted by their first ID, then by their second ID.
Course names are case-sensitive: "Art" and "art" are different courses.
Function
sharedCourses(enrollments: String[][]) → String[][]Examples
Example 1
enrollments = [["12","Databases"],["7","Calculus"],["12","Calculus"],["30","Databases"],["7","Databases"],["30","Poetry"]]return = [["7","12","Calculus","Databases"],["7","30","Databases"],["12","30","Databases"]]Students 7, 12 and 30 appear. Compared as numbers, 7 is the smallest, so its rows come first. Students 7 and 12 both take "Calculus" and "Databases". Every pair shares "Databases"; only student 30 takes "Poetry".
Example 2
enrollments = [["9","art"],["10","Music"],["9","Music"],["10","art"],["4","Chess"]]return = [["4","9"],["4","10"],["9","10","Music","art"]]Student 4 shares nothing with anyone, but both of its pairs still get a row with just the two IDs. "9" comes before "10" because IDs are compared as numbers. Students 9 and 10 share two courses; "Music" comes before "art" because uppercase letters have smaller character codes than lowercase letters.
Example 3
enrollments = [["5","Physics"],["5","Chemistry"]]return = []Only one student appears, so there is no pair and the answer is empty.
Example 4
enrollments = [["1","Art"],["2","art"],["2","Art"],["1","History"],["2","History"]]return = [["1","2","Art","History"]]"Art" and "art" are different courses. Student 1 takes "Art" and "History", and student 2 takes both of them as well, so those two are shared. Student 2's "art" is not.
Constraints
1 <= enrollments.length <= 500enrollments[i].length == 2enrollments[i][0]is a student ID: an integer from0to109written in digits with no leading zeros.enrollments[i][1]is a course name of1to30characters: English letters, digits and spaces, with no space at the start or end and no two spaces in a row.- No row appears twice.
- At most
50different students appear.
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
- Group AnagramsWorkday · PHONE SCREEN
- Group AnagramsEPAM · PHONE SCREENEPAM interview questions
- Find Duplicate File GroupsAnthropic · PHONE SCREENAnthropic interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Group AnagramsMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
- Stable Top K Frequent WordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Group AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Find Duplicate Image FilesAbnormal Security · PHONE SCREEN
- Credit Card Command ProcessorChime · PHONE SCREEN
- Employee Salary Report by GroupRippling · PHONE SCREENRippling interview questions