Created
May 31, 2016 06:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
You are provided two library functions to help you
Write a function that provides a ranked (high to low) list of courses (course IDs) for a provided user.