Created
August 27, 2019 22:15
-
-
Save BenjaminAdams/ad71e9fda92cc7260889239c06c8f115 to your computer and use it in GitHub Desktop.
Amazon Interview question
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
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