FastPrepStudent and Course Enrollment

Student and Course Enrollment

Wells Fargo logoWells Fargo● EasyINTERNOA

Problem statement

Build a small enrollment system with students and courses. You get the calls as a list of operation strings, in order:

  • "enroll student course" adds the student to the course. If the student is already in the course, nothing changes.
  • "disenroll student course" removes the student from the course. If the student is not in the course, nothing changes.
  • "get_students course" returns the names of the students now in the course, in the order they enrolled.
  • "get_courses student" returns the names of the courses the student is now in, in the order the student enrolled in them.

A student who disenrolls and later enrolls again counts as a new enrollment, so the student and the course each move to the end of the other's list.

A name with no current enrollments returns an empty list. Student names and course names are separate, so a student and a course can have the same name without affecting each other.

Return one row for each get_students or get_courses operation, in the order those operations appear. You can use any classes you like, such as Student and Course.

Function

runEnrollment(operations: String[]) → String[][]

Examples

Example 1

operations = ["enroll ana math","enroll ben math","enroll ana art","get_students math","get_courses ana","disenroll ana math","get_students math","get_courses ana"]return = [["ana","ben"],["math","art"],["ben"],["art"]]

ana and ben join math, and ana also joins art.

  • get_students for math returns ana then ben, the order they enrolled.
  • get_courses for ana returns math then art.
  • After ana leaves math, the course has only ben, and ana has only art.

Example 2

operations = ["enroll ana math","enroll ben math","enroll ana math","get_students math","disenroll cara math","disenroll ana art","get_students math","disenroll ana math","enroll ana math","get_students math"]return = [["ana","ben"],["ana","ben"],["ben","ana"]]
  • The second enroll of ana in math changes nothing, so the first result is ana then ben.
  • cara is not in math and ana is not in art, so neither disenroll changes anything. The second result is the same as the first.
  • ana then leaves and enrolls again, which moves ana to the end. The third result is ben then ana.

Example 3

operations = ["get_students math","enroll math art","get_students math","get_courses math","get_students art","disenroll math art","get_courses math"]return = [[],[],["art"],["math"],[]]
  • No one has enrolled yet, so the first get_students returns an empty row.
  • Next, a student named math joins the course art. There is still no course named math with students, so get_students for math is empty.
  • get_courses for the student math returns art, and get_students for art returns math.
  • After the student leaves, get_courses for math is empty again.

Constraints

  • 1 <= operations.length <= 1000
  • Every operation is exactly "enroll student course", "disenroll student course", "get_students course", or "get_courses student", with single spaces between the parts.
  • Every student and course name has 1 to 10 characters, and each character is a lowercase English letter or a digit.
  • At least one operation is get_students or get_courses.
  • All returned rows together hold at most 2000 names.

More Wells Fargo problems

See Wells Fargo hiring insights
Saved
public String[][] runEnrollment(String[] operations) {
    // Write your solution here.
}
Checking account…