Shared Courses for Every Pair of Students
Updated
Problem statement
A university wants to help students find courses they share with friends. Its system lists course enrollments as [studentId, course] rows.
For every pair of different students that appear in enrollments, find all the courses that both of them take. Return one row per pair: [firstId, secondId, course1, course2, ...]. Pairs that share no course are included too, and their row holds only the two IDs.
Order the result so that the answer is unique:
- In each row, the smaller student ID (compared as numbers) comes first, and the shared courses are sorted by character code, as in standard string sorting.
- Rows are sorted by their first ID, then by their second ID, both compared as numbers.
Function
sharedCourses(enrollments: String[][]) → String[][]Examples
Example 1
enrollments = [["58","Linear Algebra"],["94","Art History"],["94","Operating Systems"],["17","Software Design"],["58","Mechanics"],["58","Economics"],["17","Linear Algebra"],["17","Political Science"],["94","Economics"],["25","Economics"],["58","Software Design"]]return = [["17","25"],["17","58","Linear Algebra","Software Design"],["17","94"],["25","58","Economics"],["25","94","Economics"],["58","94","Economics"]]- The students are
17,25,58and94, so there are 6 pairs. - Students
17and58both take Linear Algebra and Software Design. - Students
25,58and94all take Economics, so each pair among them shares it. - Pairs
17, 25and17, 94share nothing, so their rows hold only the two IDs.
Example 2
enrollments = [["0","Advanced Mechanics"],["0","Art History"],["1","Course 1"],["1","Course 2"],["2","Computer Architecture"],["3","Course 1"],["3","Course 2"],["4","Algorithms"]]return = [["0","1"],["0","2"],["0","3"],["0","4"],["1","2"],["1","3","Course 1","Course 2"],["1","4"],["2","3"],["2","4"],["3","4"]]There are 5 students and 10 pairs. Only students 1 and 3 share courses: Course 1 and Course 2. Every other pair shares nothing.
Example 3
enrollments = [["23","Software Design"],["3","Advanced Mechanics"],["2","Art History"],["33","Another"]]return = [["2","3"],["2","23"],["2","33"],["3","23"],["3","33"],["23","33"]]No two students take the same course, so all 6 rows hold just two IDs. IDs are ordered by number: 2 before 3 before 23 before 33.
Constraints
1 <= enrollments.length <= 300- Each row is
[studentId, course]. A student ID is an integer from0to10^9written in digits with no leading zeros. - 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.
- No row appears twice. At most
30different students appear, and each student takes at most10courses.
More Crunchyroll Phone Screen questions
Similar questions at other companies
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Remove Later Anagrams From a ListBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Employees With Shared InterestsPalantir · PHONE SCREENPalantir interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart 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
- Find Duplicate Image FilesAbnormal Security · PHONE SCREEN