Microsoft Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




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

with latest technologies , we cna use nodejs for event driven , async functionality (ideally nodejs enables us to use javascript at server end , its a google pet) . and sip for session initiation , and using observer pattern for change listening , we can code multi chat even .

- gopi.komanduri July 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Chat room -> real time message passing system.

Entities -> Users, connections.

Message Passing system -

/sender/receiver/send_message -> message body as payload
/user/isactive

- Anonymous September 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.util.List;
class SetFrame
{
	public SetFrame(JFrame f)
	{
		f.setSize(600,500);
		f.setResizable(true);
		f.setLayout(new BorderLayout());
	}
}
class Receive extends Thread 
{	
	private Socket s;
	private JTextArea ta;
	private InputStream in;
	private JList jlist;
	private BufferedReader br;
	private String name;
	private HashMap<String,JTextArea> nameAndTextArea;
	private HashMap<String,JFrame> nameAndJFrame;
	private HashMap<JTextField,String> jtextfieldAndName;
	private ActionTextFieldPrivate actionTextField;
	private HashMap<JTextField,JTextArea> textfieldAndTextArea;
	public Receive(JTextArea ta,Socket s,String name,JList jlist,HashMap<String,JTextArea> nameAndTextArea,HashMap<String,JFrame> nameAndJFrame,HashMap<JTextField,String> jtextfieldAndName,ActionTextFieldPrivate actionTextField,HashMap<JTextField,JTextArea> textfieldAndTextArea) throws IOException
	{
		System.out.println("in A thread of Receive");
		this.s=s;
		this.textfieldAndTextArea=textfieldAndTextArea;
		this.actionTextField=actionTextField;
		this.jtextfieldAndName=jtextfieldAndName;
		this.nameAndTextArea=nameAndTextArea;
		this.nameAndJFrame=nameAndJFrame;
		this.name=name;
		this.jlist=jlist;
		this.ta=ta;
		in=s.getInputStream();
		br=new BufferedReader(new InputStreamReader(in));
		this.start();
		System.out.println("in A thread of Receive+At End");
	}
	public void run() 
	{
		while(true)
		{
			String str="";
			String data="";
			try
			{
				str=br.readLine();				
				try
				{
					data=str.substring(0,6);
				}
				catch(StringIndexOutOfBoundsException e)
				{
					System.out.println(e);
				}
				if(data.equals("delete"))
				{
					System.out.println("Delete delete delete=="+str);
					String nn=str.substring(6,str.length());
					JFrame l=nameAndJFrame.get(nn);
					l.setEnabled(false);
				}
				if(data.equals("online"))
				{
					str=str.substring(6,str.length());
					Vector<String> v=new Vector<String>();
					while(true)
					{
						try
						{
						int index=str.indexOf(",");
						String name=str.substring(0,index);
						if(!name.equals(this.name))
							v.add(name);
							str=str.substring(index+1,str.length());
						}
						catch(StringIndexOutOfBoundsException e)
						{
							break;
						}
					}
					jlist.setListData(v);
					continue;
				}
				else if(data.equals("privat"))
				{
					String nam;
					int index=str.indexOf(":");
					nam=str.substring(6,index);
					System.out.println("Send This Message to "+nam);
					str=str.substring(6,str.length());
					index=str.indexOf(":");
					int index2=str.indexOf(",");
					String str1=str.substring(0,index);
					String str2=str.substring(index2+1,str.length());
					str=str1+":"+str2;
					JFrame f=nameAndJFrame.get(nam);
					if(f==null)
					{
						JFrame frame=new JFrame("online "+":"+nam);
						frame.setSize(400,400);
						JTextArea ta=new JTextArea();
						JTextField tf=new JTextField();
						tf.addActionListener(actionTextField);
						frame.add(new JScrollPane(ta));
						ta.setText(ta.getText()+str);
						frame.add(tf,"South");
						frame.setVisible(true);
						nameAndTextArea.put(nam,ta);
						nameAndJFrame.put(nam,frame);
						jtextfieldAndName.put(tf,nam);
						textfieldAndTextArea.put(tf,ta);
					}
					else
					{
						JTextArea tta=nameAndTextArea.get(nam);
						tta.setText(tta.getText()+"\n"+str);
						f.setEnabled(true);
						f.setVisible(true);
					}
				}
				else
				{
					System.out.println(str);
					ta.setText(ta.getText()+"\n"+str);
				}
			}
			catch(IOException io)
			{
				System.out.println(io);
			}
		}
	}
}
class Send
{
	private Socket s;
	private OutputStream out;
	private PrintWriter pr;
	public Send(Socket ss,String str) throws IOException 
	{
		this.s=ss;
		out=s.getOutputStream();
		pr=new PrintWriter(out,true);
		pr.println(str);
		System.out.println("Send Message"+str);
	}
}
class ActionTextFieldPrivate implements ActionListener
	{
		private String name;
		private Socket s;
		private HashMap<JTextField,String> textfieldAndName;
		private HashMap<JTextField,JTextArea> textfieldAndTextArea;
		ActionTextFieldPrivate(String name,Socket s,HashMap<JTextField,String> textfieldAndName,HashMap<JTextField,JTextArea> textfieldAndTextArea)
		{
			this.textfieldAndTextArea=textfieldAndTextArea;
			this.name=name;
			this.textfieldAndName=textfieldAndName;
			this.s=s;
		}
		public void actionPerformed(ActionEvent ae)
		{
			JTextField tf=(JTextField)ae.getSource();
			JTextArea ta=(JTextArea)textfieldAndTextArea.get(tf);
			ta.setText(ta.getText()+"\n"+name+":"+tf.getText());
			String msg="privat"+name+":"+(String)textfieldAndName.get(tf)+","+tf.getText();
			tf.setText("");
			try
			{
				new Send(s,msg);
			}
			catch(IOException e)
			{
				System.out.println("Error in sending private message.");
			}
			
		}
	}
class User
{
	private JFrame f;
	private JTextField tf;
	private JTextArea ta;
	private Socket s;
	private	String name;
	private JList jlist;
	private DefaultListModel listModel=new DefaultListModel();
	private HashMap<String,JFrame> nameAndFrame=new HashMap<String,JFrame>();
	private HashMap<String,JTextArea> nameAndTextArea=new HashMap<String,JTextArea>();
	private HashMap<JTextField,String> textfieldAndName=new HashMap<JTextField,String>();
	private HashMap<JTextField,JTextArea> textfieldAndTextArea=new HashMap<JTextField,JTextArea>();
	private ActionTextFieldPrivate actionTextField;
	class WindowAction extends WindowAdapter
	{
		public void windowclosed(WindowEvent we)
		{
			System.out.println("closed");
			try
			{
				new Send(s,"delete"+name);
				System.exit(0);
			}
			catch(IOException e)
			{
				
			}
		}
		public void windowClosing(WindowEvent we)
		{
			System.out.println("closing");
			try
			{
				new Send(s,"delete"+name);
				System.exit(0);
			}
			catch(IOException e)
			{
			
			}
		}
	}
	private void init()
	{
		f=new JFrame();
		f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		f.addWindowListener(new WindowAction());
		tf=new JTextField();
		ta=new JTextArea();
		jlist=new JList(listModel);
	}
	private void design()
	{
		JPanel p=new JPanel();
		JMenuBar mb=new JMenuBar();
		JMenu File=new JMenu("File");
		mb.add(File);
		f.add(mb,"North");
		tf.setText("Press Enter to send Message");
		tf.addActionListener(new ActionTextField());
		tf.setBackground(Color.green);
		f.add(tf,"South");
		ta.setBackground(Color.gray);
		ta.setForeground(Color.white);
		ta.setEnabled(false);
		f.add(new JScrollPane(ta));
		p.setLayout(new BorderLayout());
		jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jlist.addMouseListener(new ButtonAction());
		p.add(jlist);
		f.add(p,"East");
	}
	class ButtonAction extends MouseAdapter
	{
		public void mouseClicked(MouseEvent ae)
		{
			if(ae.getClickCount()==2)
			{
				System.out.print("you Doubled Clicked on");
				String chatGuy=(String)jlist.getSelectedValue();
				System.out.println(chatGuy);
				if(!nameAndFrame.containsKey(chatGuy))
				{
					JFrame frame=new JFrame("online "+":"+chatGuy);
					frame.setSize(400,400);
					JTextArea ta=new JTextArea();
					JTextField tf=new JTextField();
					tf.addActionListener(actionTextField);
					frame.add(new JScrollPane(ta));
					frame.add(tf,"South");
					frame.setVisible(true);
					nameAndTextArea.put(chatGuy,ta);
					nameAndFrame.put(chatGuy,frame);
					textfieldAndName.put(tf,chatGuy);
					textfieldAndTextArea.put(tf,ta);
				}
				else
				{
					JFrame frame=nameAndFrame.get(chatGuy);
					frame.setVisible(true);
				}
			}
		}
	}
	/*
	class ActionTextFieldPrivate implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			JTextField tf=(JTextField)ae.getSource();
			String msg="privat"+name+":"+(String)textfieldAndName.get(tf)+","+tf.getText();
			tf.setText("");
			try
			{
				new Send(s,msg);
			}
			catch(IOException e)
			{
				System.out.println("Error in sending private message.");
			}
			
		}
	}
	*/
	class ActionTextField implements ActionListener
	{
		public void actionPerformed(ActionEvent Ae) 
		{
			String str=tf.getText();
			System.out.println(name);
			tf.setText("");
			try
			{
				new Send(s,name+":"+str);
			}
			catch(IOException io)
			{
				System.out.println(io);
			}
		}
	}
	public User() throws IOException
	{
		name=JOptionPane.showInputDialog(f,"Enter Name:");
		System.out.println(name);
		s=new NameSocket(name,InetAddress.getByName("localhost"),2020);
		actionTextField=new ActionTextFieldPrivate(name,s,textfieldAndName,textfieldAndTextArea);
		OutputStream out=s.getOutputStream();
		PrintWriter pr=new PrintWriter(out,true);
		pr.println("@@@---"+name+"@@@---");
		init();
		new Receive(ta,s,name,jlist,nameAndTextArea,nameAndFrame,textfieldAndName,actionTextField,textfieldAndTextArea);
		new SetFrame(f);
		design();
		f.setVisible(true);
	}
	public static void main(String arg[]) throws IOException
	{
		new User();
	}
}
class RegisterThread extends Thread
{
	private InputStream in;
	private OutputStream out;
	private BufferedReader br;
	private List<Socket> arrList;
	private Socket s;
	private List<String> arrName;
	private HashMap<String,Socket> nameAndSocket;
	public RegisterThread(Socket s,Vector<Socket> arrList,Vector<String> arrName,HashMap<String,Socket>nameAndSocket) throws IOException
	{
		this.arrName=arrName;
		this.nameAndSocket=nameAndSocket;
		this.s=s;
		this.arrList=arrList;
		in=s.getInputStream();
		br=new BufferedReader(new InputStreamReader(in));
		this.start();
	}
	public void run()
	{
		while(true)
		{
			String str="";	
				try
				{
				str=br.readLine();
				}
				catch(Exception e)
				{
					return;
				}
				if(str.equals("") || str.equals("\n"))
					continue;
				String temp="";
				System.out.println(str);
				try
				{
					temp=str.substring(0,6);
				}
				catch(StringIndexOutOfBoundsException se)
				{
					System.out.println(se);
				}
				if(temp.equals("delete"))
				{
					synchronized (arrName)
					{
					System.out.println("Delete");
					String del=str.substring(6,str.length());
					try
					{
					Socket ll=nameAndSocket.get(del);
					OutputStream out=ll.getOutputStream();
					PrintWriter pr=new PrintWriter(out,true);
					pr.println(str);
					}
					catch(Exception e)
					{
						System.out.println("new Error");
					}
					arrName.remove(del);
					nameAndSocket.remove(del);
					return;
					}
				}
				if(temp.equals("@@@---"))
				{
					str=str.substring(6,str.length());
					int index=str.lastIndexOf("@@@---");
					temp=str.substring(0,index);
					nameAndSocket.put(temp,s);
					arrName.add(temp);
					System.out.println("new user adding to the arrName"+temp);
				}
				else if(temp.equals("privat"))
				{
					//str=str.substring(6,str.length());
					int index1=str.indexOf(":");
					int index2=str.indexOf(",");
					Iterator k=nameAndSocket.keySet().iterator();
					System.out.println("private Message so traversing ");
					while(k.hasNext())
					{
						System.out.println((String)k.next());
					}
					temp=str.substring(index1+1,index2);
					System.out.println("private Message recieved for"+temp);
					System.out.println(nameAndSocket.containsKey(temp));
					try
					{
						Socket ss1=nameAndSocket.get(temp);
						if(ss1==null)
						{
							System.out.println("null in socket");
						}
						OutputStream out=ss1.getOutputStream();
						PrintWriter pr=new PrintWriter(out,true);
						pr.println(str);
					}
					catch(IOException io)
					{

					}
				}
				else
				{
					Iterator ii=arrList.iterator();
					while(ii.hasNext())
					{
						Socket s1=(Socket)ii.next();
						{
							try
							{
							out=s1.getOutputStream();
							PrintWriter pr=new PrintWriter(out,true);
							pr.println(str);
							}
							catch(IOException io)
							{
								System.out.println("Socketdel");
							}
						}
					}
				}
		}
	}
}
class OnlineThread extends Thread
{
	Vector<String> arrName;
	Vector<Socket> arrSocket;
	OnlineThread(Vector<String> arrName,Vector<Socket> arrSocket)
	{	
		this.arrName=arrName;	
		this.arrSocket=arrSocket;
		this.start();
	}
	public void run()
	{
		while(true)
		{
			
			Iterator i=arrName.iterator();
			String data="";
			try
			{
			while(i.hasNext())
			{
				data=data+(String)i.next()+",";
			}
			}
			catch(ConcurrentModificationException e)
			{
				System.out.println("Concuent but runing");
			}
			data="online"+data;
			for(int ii=0;ii<arrSocket.size();ii++)
			{
				Socket s=arrSocket.get(ii);
				try
				{
					OutputStream out=s.getOutputStream();
					PrintWriter pr=new PrintWriter(out,true);
					pr.println(data);
				}
				catch(IOException E)
				{
					System.out.println(E);					
				}
			}
		}
	}
}
class Server
{
	private ServerSocket ss;
	private Socket s;
	private Vector<Socket> arrList=new Vector<Socket>();
	private Vector<String> arrName=new Vector<String>();
	private HashMap<String,Socket> nameAndSocket=new HashMap<String,Socket>();
	public Server() throws IOException
	{
		ss=new ServerSocket(2020);	
		new OnlineThread(arrName,arrList);
		while(true)
		{
			s=ss.accept();
			arrList.add(s);
			String str="";
			new RegisterThread(s,arrList,arrName,nameAndSocket);
		}
	}
	public static void main(String arg[]) throws IOException
	{
		new Server();
	}
}

- nikesh September 26, 2014 | 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