Amazon Interview Question
Software Engineer / DevelopersClass structure would look something like this:
User {
String name;
//other properties
list<Post> posts;
list<User> subscribers;
public void getAllMessageOfUser(User user) {
if(subscribers == null)
subscribers = new List<User>();
subscribers.add(user);
}
public List<User> getSubscriptionList(){
return subscribers;
}
}
Post {
String text;
User owner;
String comment;
public void postMessage(String text, User user) {
this.owner = user;
this.text = text;
for(User u : user.getSubscriptionList()) {
u.notify(user, text);
}
}
}
Here we can use Observer pattern.
Each message will be stored in some persistance unit with schema like
- Hinax December 13, 2010Message: postedByUserId, MessageId, MessageText, MessagePostTime
User: userId, userName, Receiver(will be having some serialized listening userIds, can be persisted as separate relation), serialized message Ids.
1) PostMessage(Reciever,Messsage): The user will be holding the reference for receiver. The receiver class will be holding the list of subscriber userIds (and might be some other action specific parameters)
Receiver will append the new message id at the end of persisted subscribed_messages column. That can be the way through which messages are published
2) getallmessageofuser(user) will query for the postedbyuserid.