Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BenjaminAdams
Created August 27, 2019 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenjaminAdams/ad71e9fda92cc7260889239c06c8f115 to your computer and use it in GitHub Desktop.
Save BenjaminAdams/ad71e9fda92cc7260889239c06c8f115 to your computer and use it in GitHub Desktop.
Amazon Interview question
A-B-C-D
|/ /
E-F
A-B C-D
|/ /
E F
//social network
//input userId/userId
//return true/false if that person is in their network
function user(name){
this.id = randomId
this.name= name
this.friends = []
this.addFriend(function(id){
if(this.friends.includes(id)) return
this.friends.push(id)
})
}
var user1= new user('ben')
var user2= new user('Tomasz')
var seen=[]
function areTheyFriends(user1,user2){
if(user1===null || user2===null || typeof(user1)==='undefined'||typeof(user2)==='undefined') return false
seen.push(user1)
if(!user1.friends || !user1.friends.length) return false
for(let i=0; i< user1.friends.length <0;i++){
if(user1.friends[i].id===user2.id) return true
if(seen.includes(user1.friends[i].id)) continue
if(areTheyFriends(user1.friends[i].id,user2)){
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment