Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Created January 31, 2020 16:20
Show Gist options
  • Save saswata-dutta/1537c2d8540347845ad0fff9280dcd27 to your computer and use it in GitHub Desktop.
Save saswata-dutta/1537c2d8540347845ad0fff9280dcd27 to your computer and use it in GitHub Desktop.
// given
def getFriends(id: Id): Set[Id] = ???
// implement suggestions for friends who have the most mutual friends with user
def suggestedFriends(id: Id, limit: Int): Set[Id] = {
require(limit > 0)
require(id != null)
val friends = getFriends(id)
val friendsOfFriends =
friends.toSeq
.flatMap(f => (getFriends(f).diff(friends) - id).toSeq)
friendsOfFriends
.groupBy(identity)
.sortBy(_._2.size)(Ordering[Int].reverse)
.take(limit)
.map(_._1)
.toSet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment