Microsoft Interview Question for Interns


Country: India
Interview Type: In-Person




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

Can make use of Observer Design Pattern.
Here is a simple implementation of the Publisher/Subscriber model for StockMarket. As and when interviewer pose with some more questions, we can add on to it.

Publisher Interface:

public interface Publisher {
  public void setStockValue(int value);
  public void registerObserver(Observer observer);
  public void unRegisterObserver(Observer observer);
  public void notifyAllObservers();
}

Subscriber(Observer) Interface:

public interface Observer {
   public void update(int newValue);
   public void display();
   public void unSubscribe();
}

StockMarket Class which implements the Publisher interface:

public class StockMarket : Publisher {
  Set<Observer> observers;
  int newValue;

  public StockMarket() {
	observers = new HashSet<Observer>();
	newValue = 0;
  }  
  
  public void setStockValue(int value){
    newValue = value;
  }
  
  public void registerObserver(Observer observer){
    observers.add(observer);
  }

  public void unRegisterObserver(Observer observer){
    if(observer != null && observer instanceof Observer){
	    observers.remove(observer);
	}
  }

  public void notifyAllObservers(){
	Iterator<Observer> observerIterator = observers.iterator();
	while(observerIterator.hasNext()){
		Observer observer = observerIterator.next();
		observer.update(this, null);
	}
  }
}

As a sample i will implement the PayPalStock which implements the Observer Interface. This inturn maintains a reference to the Publisher.

public class PayPalStock : Observer {
   Publisher stockMarket;
   int latestValue;

   public PayPalStock (Publisher subject){
   	latestValue = 0;
    stockMarket = subject;
    stockMarket.registerObserver(this); // Self-Registering to the Publisher
   }

   public void update(int newValue){
    latestValue = newValue;
    display();
   }

   public void display(){
     System.out.println("Latest Stock Price = " + latestValue);
   }

   public void unSubscribe(){
     stockMarket.unRegisterObserver(this);
   }
}

A more detailed design can be looked up at: http://read.pudn.com/downloads152/doc/663610/Software%20Design.pdf

- R@M3$H.N January 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A market is a place where buy and sell happens real-time or in a batch. depending on the service... The code above won't cut....

- sarnath January 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stock Market System is about a "Market" - i.e. Buying and Selling.
Your solution should talk about how you will satisfy the order, will that be atomically satisfied or not, How will you avoid starvation of orders, What happens to huge orders?, Strategy for that... All that would matter in a potential solution.

- Sarnath January 24, 2017 | Flag Reply


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