Microsoft Interview Question
SDE-2sCountry: United States
Interview Type: Phone Interview
Hey, I think you misunderstood the question, We don't have to print out the properties we have to rent out the messages send to the queue.
Oh I see, so do you mean like you can send queue messages and then it immediately prints whatever messages sent to the queue? Like would an example be like:
q = Queue()
q.issueMessage("Message 1")
q.issueMessage("Message 2")
and an implicit listener would analyze the incoming message for the queue and print
Message 1 printed
Message 2 printed
Also would it be printed when an element is dequeued or enqueued to the queue? Can you please give an example, Sameer?
I hope this is correct and match the question :)
Code: https://github.com/tabvn/ued/blob/master/careercup/Microsoft/SDE-2s.cpp
Output:
New subscriber:
Username: admin
Password: admin
The queue is running on port: 8161
Received message:New message.
Received message:New message.
Received message:New message.
Received message:New message.
Received message:New message.
#include <iostream>
#include <thread>
#include <vector>
#include <string>
#include <chrono>
struct Subscriber
{
std::string username;
std::string password;
void(*callback)(std::string message);
Subscriber(std::string username, std::string password, void(*cb)(std::string message) ){
this->username = username;
this->password = password;
this->callback = cb;
}
};
struct Queue{
std::string name;
std::vector<Subscriber*> subscribers;
Queue(std::string name){
this->name = name;
}
};
struct Event{
std::vector<Queue*> queues;
Queue* addQueue(std::string name){
Queue *q = new Queue(name);
this->queues.push_back(q);
return q;
}
Queue *getQueue(std::string name){
for (int i = 0; i < this->queues.size(); ++i){
if(this->queues[i]->name == name){
return this->queues[i];
}
}
return NULL;
}
void addSubscriber(Queue *q, Subscriber *subscriber){
q->subscribers.push_back(subscriber);
// Print message when new subscriber joined.
std::cout << "New subscriber: "<< std::endl;
std::cout << "Username: " << subscriber->username << std::endl;
std::cout << "Password: " << subscriber->password << std::endl;
}
void publish(std::string message, std::string queueName){
Queue *queue = this->getQueue(queueName);
if(queue != NULL){
for (int i = 0; i < queue->subscribers.size(); ++i){
queue->subscribers[i]->callback(message);
}
}
}
void listen(int port){
std::cout << "The queue is running on port: " << port << std::endl;
while(true){
}
}
};
/*
* This is function callback when receive message of the queue
*/
void messageReceivedCallback(std::string message){
std::cout << "Received message:" << message << std::endl;
}
Event even;
/**
* Start event service and wait for message
**/
void runEvent(){
Queue *queue = even.addQueue("Demo"); // create new queue demo for subscribers
//New subscriber
Subscriber *subscriber = new Subscriber("admin", "admin", messageReceivedCallback);
even.addSubscriber(queue, subscriber);
even.listen(8161);
}
/**
*/
void pushMessage(){
// loop and send message every 2 seconds
while(true){
std::this_thread::sleep_for (std::chrono::seconds(2));
even.publish("New message.", "Demo");
}
}
int main(int argc, char const *argv[]){
std::thread eventThread(runEvent);
std::thread pushMessageThread(pushMessage);
eventThread.join();
pushMessageThread.join();
return 0;
}
Java has a very easy implementation to subscribing a queue. Using annotation
We can pull all the properties: port, hostname, virtual host, username, password in application.yml
@RabbitListener("Queue_Name")
public void listen(Message message) {
//unmarshal it to the object here and perform operations.
}
I believe this question tests knowledge on the Observer Design pattern. Implementations across languages vary but I present a solution in Swift (iOS) below using Property Observers.
Output
- prudent_programmer December 02, 2018