Skip to content

Instantly share code, notes, and snippets.

@h2rashee
Created May 31, 2016 06:23
Show Gist options
  • Save h2rashee/f391f6a08754bd8d7287661958f3cee9 to your computer and use it in GitHub Desktop.
Save h2rashee/f391f6a08754bd8d7287661958f3cee9 to your computer and use it in GitHub Desktop.
Given a graph of users, determine the best course suggestions to give the central user based on the spec
/*
* import java.util.*;
* import java.io.*;
*
* public List<String> getDirectFriendsForUser(String user)
*
* public List<String> getAttendedCoursesForUser(String user)
*
* Please complete the method below
*/
public List<String> getRankedCourses(String user) {
HashSet<String> socialCircle = new HashSet<String>();
List<String> friends = getDirectFriendsForUser(user);
socialCircle.addAll(friends);
List<String> myCourses = getAttendedCoursesForUser(user);
// Who is in my social circle?
for(int i = 0; i < friends.size(); i++) {
socialCircle.addAll(getDirectFriendsForUser(friends.get(i)));
}
Hashtable<String, Integer> courseCount = new Hashtable<String, Integer>();
// Let's find the courses of people in my social circle and tabulate it
for(String u : socialCircle) {
List<String> courses = getAttendedCoursesForUser(u);
for(int j = 0; j < courses.size(); j++) {
String course = courses.get(j);
if(courseCount.containsKey(course)) {
courseCount.put(course, courseCount.get(course) + 1);
} else {
courseCount.put(course, 1);
}
}
}
ArrayList<Map.Entry<String, Integer>> sorted = sortValue(courseCount);
List<String> courseRankings = new ArrayList<String>();
// Translate the sorted list to courses I haven't taken
// and I'd likely want as suggestions
for(int i = 0; i < sorted.size(); i++) {
String course = sorted.get(i).getKey();
if(!myCourses.contains(course)) {
courseRankings.add(course);
}
}
return courseRankings;
}
// Take a Hashtable and translate it into an ArrayList sorted by Hashtable value
public static ArrayList<Map.Entry<String, Integer>> sortValue(Hashtable<String, Integer> t){
ArrayList<Map.Entry<String, Integer>> l = new ArrayList(t.entrySet());
Collections.sort(l, new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}});
return l;
}
@h2rashee
Copy link
Author

Suppose you are an engineer. Your team wants to launch a new feature called “Courses Your Social Network have Attended,” that lists all courses that your social network are taking, sorted by popularity.

A social network is defined as all direct friends and all direct friends of direct friends. People three levels deep are not part of the social circle.

The recommendation logic is based on the following rules:

  • A user should only be recommended a course that their social network have attended but they have not attended
  • The recommendations priority is driven by how many people have attended the same course – if multiple people attended the same course, it should be higher in the recommendations than a course that only one person attended.
    You are provided two library functions to help you
  • getDirectFriendsForUser – returns a list of customer IDs (strings that uniquely identify an Amazon user) representing the direct friends of an Amazon user
  • getAttendedCoursesForUser – returns a list of course IDs (strings that uniquely identify a course) for an Amazon user ordered by attendance time with newest course first in list and oldest course last in list

Write a function that provides a ranked (high to low) list of courses (course IDs) for a provided user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment