Microsoft Interview Question for SDE-2s

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
of 0 vote

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.

import Foundation
import Queue

class Demo {

  var portNumber: Int16
  var userName: String
  var password: String
  var queue: Queue? {
    didSet {
  init(portNumber: Int16, userName: String, password: String) {
    self.portNumber = portNumber
    self.userName = userName
    self.password = password
  private func printListenerMessage() {
    let message = """
    The queue is running on port : \(portNumber)
    Username : \(userName)
    Password : \(password)

// In Main.swift
let demoApp = Demo.init(portNumber: 8161, userName: "admin", password: "admin")
demoApp.queue = Queue()


The queue is running on port : 8161
Username : admin
Password : admin

- prudent_programmer December 02, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 votes

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.

- Sameer December 03, 2018 | Flag
Comment hidden because of low score. Click to expand.
of 0 votes

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?

- prudent_programmer December 04, 2018 | Flag
Comment hidden because of low score. Click to expand.
of 0 vote

I hope this is correct and match the question :)


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);
		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){


		// 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){


	void listen(int port){
		std::cout << "The queue is running on port: " << port << std::endl;


* 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);


void pushMessage(){

	// loop and send message every 2 seconds

		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);

	return 0;

- December 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

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

public void listen(Message message) {
//unmarshal it to the object here and perform operations.

- Sameer July 21, 2019 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More