Suppose you have created a social networking application, in which users can sign-up and be friends with other users. Your application has 3 APIs:
add_user(email) - adds user to the system.
befriend(email1, email2) - creates a friendship between the two given users.
check_friendship(email1, email2) - checks whether the two given users are directly or indirectly friends. Indirect friends meaning they have a connectivity via their friends of friends of... friends.
Constraint: The check_friendship API should take O(1) time.
Looking for simple answer for this coding problem -
You have a website with different products, sub-categories, their sub-categories and so-on.
for eg.-
A,1
F,1
T,2
A,1 > B,2
A,1 > C,3
A,1 > E,3
F,1 > G,4 > H,7 > I,9
F,1 > K,3
T,2 > Z,100
Where A, F, T are the categories and rest sub-categories and so on.
You have to return number of total products in each product.
A = 9
B = 2
C = 2 ....
I try using tree and trie. Anyone can do this in simple hashing techinque.
Anyone tell the approach and code for both the questions in python.
thanks