Bloomberg | Phone | Design a mailbox
Question Details
Hi everyone, I had this problem on my interview with Bloomberg. Design a mailbox which should do the two functions: 1. send a message. 1. retrieve all messages of a user. After I finished...
Full Details
Hi everyone,
I had this problem on my interview with Bloomberg.
Design a mailbox which should do the two functions:
1. send a message.
1. retrieve all messages of a user.
After I finished the requirements, he asked me to extend the code to provide these two functions:
1. send an error message if a receiver doesn\'t exist.
1. send one message to multiple users.
The problem was very straighforward and easy. I was able to communicate my thoughts and ideas with the interviewer. But after I finished the interview, and I felt that I will proceed to the next stage, I realized that my code could be better and improved. I guess the time constraint of the interview, and the overall stress were playing against me.
For example: (1) create a new class to save users, and make it a singelton class which has a set to store the users. (2) have a methods which validates if a user exists or not.
I will put my code here, and I am happy to receive your comments about it, and if you think I will pass it or not? Probably will get the feedback after the Christmas holidays. Merry Christmas everyone :)
import java.security.Timestamp;
import java.util.*;
public class MainClass {
\tstatic Set<User> userDB;
\t
\tpublic static void main(String[] args) {
\t\tuserDB = new HashSet<User>();
\t\tUser user1 = new User();
\t\tUser user2 = new User();
\t\tuserDB.add(user1);
\t\tuserDB.add(user2);
\t\t
\t\tMessage msg = new Message();
\t\tmsg.sender = user1;
\t\tmsg.receiever = user2;
\t\tmsg.body = "Hello";
\t\tmsg.timeSent = Timestamp.class.cast(" ");
\t\tuser1.mailBox.send(msg, user2);
\t}
}
class Message{
\tUser sender;
\tUser receiever;
\tString body;
\tTimestamp timeSent;
}
class User{
\tString name;
\tString email;
\tMailBox mailBox;
}
class MailBox{
\tList<Message> inbox = new ArrayList<Message>();
\tpublic void sendMultipleUsers(Message message, List<User> receiverList) {
\t\tfor(User receiver : receiverList) {
\t\t\tsend(message, receiver);
\t\t}
\t}
\t\t
\tpublic void send (Message message, User receiver) {
\t\tif(MainClass.userDB.contains(receiver)) {
\t\t\treceiver.mailBox.inbox.add(message);
\t\t}
\t\telse {
\t\t\tMessage msgError = new Message();
\t\t\tmsgError.sender.name = "Global System";
\t\t\tmsgError.receiever = null;
\t\t\tmsgError.body = "Error";
\t\t\tmsgError.timeSent = Timestamp.class.cast(" ");
\t\t\tinbox.add(msgError);\t
\t\t}
\t}
\t
\tpublic List<Message> retreiveAll(){
\t\treturn inbox;
\t}
}
About This Question
This is a reported interview question from a bloomberg interview for a swe role during the system design round reported in 2023.
It covers the following topics: Hash Table, Arrays, System Design, Strings .
Difficulty rating: Easy