Microsoft Interview Question for SDE-2s


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
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 {
      printListenerMessage()
    }
  }
  
  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)
    """
    print(message)
  }
}

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

Output

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.
0
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.
0
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


Add a Comment
Name:

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

Books

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

Videos

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