FastPrepCourses Shared by Every Student Pair

Courses Shared by Every Student Pair

Robinhood logoRobinhood● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Each row of enrollments is [studentName,courseName]. For every pair of distinct students, return the courses they both take.

For this exercise, assume duplicate enrollments have no effect, and only students appearing in the input are represented. Compare names and courses as complete case-sensitive strings.

For this exercise, return every unordered pair once, including pairs with no shared course. Each result row is [firstStudent,secondStudent,course1,...], with its two names in lexicographic order followed by sorted unique common courses. A pair without a common course has exactly two fields.

For this exercise, assume result rows are ordered lexicographically by their student-name pair. Empty input or one represented student returns an empty array.

Function

sharedCoursePairs(enrollments: String[][]) → String[][]

Examples

Example 1

enrollments = [["A","Math"],["A","Art"],["B","Math"],["C","Art"]]return = [["A","B","Math"],["A","C","Art"],["B","C"]]

These authored rows include every pair; B/C has no shared course and returns only its two names.

Example 2

enrollments = [["Alice","Math"],["Alice","Math"],["Bob","Math"],["Bob","Art"]]return = [["Alice","Bob","Math"]]

This authored repeated enrollment contributes one shared Math entry.

Constraints

  • For this exercise, assume 0 <= enrollments.length <= 120, with exactly two strings per row and at most 20 distinct students.
  • For this exercise, assume all names are nonempty case-sensitive printable ASCII strings of length 1 through 10.
  • Repeated enrollment rows may appear and have no additional effect.

More Robinhood problems

See Robinhood hiring insights
public String[][] sharedCoursePairs(String[][] enrollments) {
    // Write your code here.
    return new String[0][0];
}
enrollments[["A","Math"],["A","Art"],["B","Math"],["C","Art"]]
expected[["A", "B", "Math"], ["A", "C", "Art"], ["B", "C"]]
Checking account…