Microsoft Interview Question


Country: India




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

for design, we may implement Publisher subscriber (Same type -- but difference instance subscription)..

using System;

namespace PubSub
{
    class Friend
    {
        private string Name;
        private int location;

        public Friend(string Name) 
        {
            location = 0;
            this.Name = Name; 
        }

        private event EventHandler OnFriendChangeLocation;
        public event EventHandler MyProperty
        {
            add { OnFriendChangeLocation += value; }
            remove { OnFriendChangeLocation -= value; }
        }

        public void Connect(Friend friend, bool subscribe = true)
        {
            if(subscribe) friend.OnFriendChangeLocation += friend_OnFriendChangeLocation;
            else friend.OnFriendChangeLocation -= friend_OnFriendChangeLocation;
        }

        public void PublishNewLocation(int location)
        {
            this.location = location;
            if (OnFriendChangeLocation != null) OnFriendChangeLocation(this, null);
        }

        protected void friend_OnFriendChangeLocation(object sender, EventArgs e)
        {
            // Compute difference of location and do logic for 2 miles here...
            Console.WriteLine(this.Name + "receive updates from "+ (sender as Friend).Name);
        }
    }
}

class Program
    {
        static void Main(string[] args)
        {
            Friend george = new Friend("George");
            Friend ana = new Friend("Ana");
            Friend billy = new Friend("Billy");
            // Subscription....
            george.Connect(ana);
            george.Connect(billy);
            ana.Connect(george);
            ana.Connect(billy);
            billy.Connect(george);
            billy.Connect(ana);

            george.PublishNewLocation(100);
            // Unsubscription...
            // .....
            Console.ReadLine();
        }
    }

In this way you don't have to maintain a list (due to subscription...).
Note: this code does not reflect the logic of location, so you may have other logic for that...
the point here is the design on how to notify others when one state change... Also on the above code, others are notified PublishNewLocation is called, however, you can implement or add a property where when this property value changed or set, then PublishNewLocation is called...

Hope this helps...

- Anonymous23 July 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Person
{
string id;
Location location;
string phoneNumber;
List<Person> friends;
}

public void sendmessage(List<Person> groupofperson)
{

foreach(Person person in groupofperson)
{
foreach(Person friend in person.friends)
{
if(getDistance(person.location, friend.location)<=givenDistance)
{
sendMessage(person);
sendMessage(friend);
}
}
}
}

- Jackie July 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To get an efficient distance function, one might divide the world to hexagons of 1 mile side.
SetLocation of person p will assign it to a hexagon s.
now to find all the people with maximum 2 miles radius you need to check your hexagon for people and the hexagons surrounding you (6 of them) and thats it.
The use of hexagon and not square is because hexagons can wrap around a globe while squares not (think about a soccer ball)

- The One September 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple Observer pattern Design
1. There are two types of classes - Person class and Friend Class.
2. Person Class should be notified only when the nearness of friend is updated. Person class should notify of its location changes to friend. Person Class should implement LocationObservable Interface and NearnessObserver Interface
3. Friend Class should be notified only when the location of the person is updated. Friend class should notify of its nearness location based on the location changes he receives from the Person.
4. Following is a simple method stub , not complete solution just class diagram :

public interface LocationObservable {
     addFriend(LocationObserver locationObserver);
     removeFriend(LocationObserver locationObserver);
     notifyLocationChangeToAll();
     notifyLocationChangeToObserver(LocationObserver locationObserver);
}

public interface LocationObserver{
     getLocationUpdates();
}

public interface NearnessObersvable{
     addNearnessNotification(Nearness Observer nearnessObserver);
     removeNearnessNotification(NearnessObserver nearnessObserver);
     notifyNearnessChange(Nearness Observer);
     notifyNearnessChangeToAll();
}

public interface NearnessOberserver{
     getNearNessUpdates();
}

public class Friend implements LocationObserver,NearnessObservable{
     private List<NearnessObserver> persons;

     addNearnessNotification(Nearness Observer nearnessObserver){

     }
     removeNearnessNotification(NearnessObserver nearnessObserver){

     }
     notifyNearnessChange(Nearness Observer){

     }
     notifyNearnessChangeToAll(){

     }

    getLocationUpdates(){

     }
}

public class Person implements NearnessOberserver, LocationObservable{
     private List<LocationObserver> friends;
     getNearNessUpdates(){
     }
     addFriend(LocationObserver locationObserver){
     }
     removeFriend(LocationObserver locationObserver){
     }
     notifyLocationChangeToAll(){
     }
     notifyLocationChangeToObserver(LocationObserver locationObserver){
     }
}

- nilesh.d.agrawal October 13, 2016 | 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