Morgan Stanley Interview Question for Associates


Country: India
Interview Type: Phone Interview




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

I solution i can think of (works well with small set of intergers, say "N"). We'll have an array of CriticalSection/ mutex defined and if a thread with particular integer call this function, this thread will take the mutex (acquire the lock) and will print the value. Other thread with same integer value will wait.

- Mahi. May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int system;
static int number;

public void run(){
System.out.println(Thread.currentThread().getName());
//System.out.println(number);
if(number ==0){
System.out.println("running runnable");
int value =new IntegerThreadCall().execute(3);
System.out.println(value);
system =value;
}else{
int value =new IntegerThreadCall().execute(3);

if(value == system){

try {
Thread.currentThread().sleep(1000000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println(value);
system=value;
}
}

number ++;
}


public static void main(String ... args){
System.out.println("here");

IntegerThreadCall ith1=new IntegerThreadCall();
ith1.start();

IntegerThreadCall ith2=new IntegerThreadCall();
ith2.start();

IntegerThreadCall ith3=new IntegerThreadCall();
ith3.start();


}
public int execute(int i){

//System.out.println(i);

return i;

}

- Anonymous May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think an implementation

{{
public void print (Integer value){
synchronized(value){
System.out.println(value);
}
}
}}

when the function called with same integer as apramaters only one thread will get lock on Integer Object

- Maximus May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are using Integer parameter as if its an enum (which is singleton), so you are assuming that if the value is same then the parameters for two different threads with same int value would be same object and synchornized() will do the trick ? The instance of Integer will be a different object for two different threads even with same value... so synchronizing on Integer would not help - It would if it was an enum ,,,

- Makarand May 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think an implementation

{{
public void print (Integer value){
synchronized(value){
System.out.println(value);
}
}
}}

when the function called with same integer as apramaters only one thread will get lock on Integer Object

- Maximus May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution would be:

1. first create a function (synchronized) to convert an integer to corresponding integer object using a map/hashtable such that this map returns same object with same integer value.

2. then print function should be like:

public void print (Integer value){ 
synchronized(value){ 
System.out.println(value); 
} 
}

I hope this would work.. :)

- coding.arya May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

for object ineterning for Integer it will work. But above 128 it will not work. Please reconsider the case.

- debnath May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the duplicate values will be printed after the lock removed..how to discard the duplicate values??

- Rakesh May 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Rakesh: The question doesn't say not to print duplicate values... It says that it should synchronize printing if the threads are going to print same value..

- coding.arya May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class Test{
private Map<Integer,String> lM=new HashMap<Integer,String>();
public static void main(String[] args){
final Test t=new Test();
Thread t1=new Thread(new Runnable(){
public void run(){

t.printNumber(1);

}

});
Thread t2=new Thread(new Runnable(){
public void run(){
t.printNumber(1);

}

});
Thread t3=new Thread(new Runnable(){
public void run(){
t.printNumber(2);

}

});
t1.start();
t2.start();
t3.start();
}
public void printNumber(int n){
String lo=stringFactory(n);
synchronized(lo){
for(int i=0;i<10;i++){
try{
Thread.sleep(1000);
}catch(Exception e){}
System.out.println(Thread.currentThread()+":"+n);
}
}
}
private String stringFactory(int n){
if(lM.get(n)==null)
lM.put(n,new String());
return lM.get(n);
}
}

- Anonymous May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package Morgan_Stanley;

import java.util.HashMap;

public class Locking_On_Same_Integer_Obj {
HashMap<Integer, String> map = new HashMap<Integer, String>();
String val;	
	public void print(Integer obj){
		if(map.get(obj)==null){
			val = obj.toString();
			map.put(obj, val);			
		}else
			val = map.get(obj);
		
		synchronized(val){
			int i =0;
			while(i<1000){
			System.out.println(Thread.currentThread().getName());
			System.out.println(obj);
			System.out.println(i);
			i++;
			if(i>100)
				break;
			}
		}
	}
	
	public static void main(String args[]){
		Locking_On_Same_Integer_Obj obj1 = new Locking_On_Same_Integer_Obj();
		Thread t1 = new Thread(new ThreadTest(130,obj1));
		Thread t2 = new Thread(new ThreadTest(130,obj1));
		t1.start();
		t2.start();
		
	}
}

class ThreadTest implements Runnable{
	
	Integer value;
	Locking_On_Same_Integer_Obj testObj;
	public ThreadTest(int k,Locking_On_Same_Integer_Obj lock){
		value = k;
		testObj = lock;
		//testObj.print(value);
	}
	
	@Override
	public void run(){
		testObj.print(value);
	}
	
}

- Anonymous May 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution is simple and fathomable. Are you with morgan Stanely?

- Jaisree April 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Idea is ok, but I dont think above code will work correctly, as synchronization is not been taken care while putting element into the Map.

- Ajay Kumar April 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about this :)
public class Test{
private AtomicReference<Integer> intVal = new AtomicReference<Integer>();

public void print(Integer i){
intVal.compareAndSet(-1, i);
System.out.println(i);
intVal.compareAndSet(i, -1);

}
}

- Mohd Adnan August 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static void main(String...str){
int a[] = {1,2,3,4,5,6,18, 50};
int b[] = {1,3,5,6,7,8, 9, 10};
int i = 0;
int j = 0;
while (i < a.length && j < b.length ){
if(a[i] > b[j ]){
System.out.print(b[j ++] + " ");
}else if(a[i] < b[j] ){
System.out.print(a[i ++] + " ");
}else if(a[i] == b[j]) {
i ++;
j ++;
}
}
while (i < a.length){
System.out.print(a[i ++] + " ");
}
while (j < b.length){
System.out.print(b[j ++] + " ");
}
}

- Mohd Adnan August 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TestConditionLock {
	
	private Lock lock = new ReentrantLock();
	private Integer value = null;
	private Condition condition = lock.newCondition();
	
	void method(final int i) throws InterruptedException
	{
		if(i == value)
		{
			System.out.println("Integer: "+ i);
		}
		else
		{
			lock.lock();try
			{
				while(value!=null && value!=i)
				{
					condition.await();
				}
				value = i;
				System.out.println("Integer: "+ i);
				value = null;
				condition.signalAll();
			}
			finally
			{
				lock.unlock();
			}
		}
	}

}

- Nitin Taur October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction value should be declared volatile

- Anonymous October 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void print(int value){
String str = new String(value).intern();
synchronized(str){
System.out.println(value);
}
}

- jane March 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void print(int value){
	String str = new String(value).intern();
        synchronized(str){ 
		System.out.println(value); 
	} 
}

- jane March 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Something like this ?

{ public void printInteger(int inputInteger){
	String string = "" + inputInteger;
	synchronized(string)
		System.out.println(string);
	}

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

What about this??

private HashMap<Integer> count=new HashMap<Integer>();
	public void printInteger(int a) {
		synchronized (count) {
			while (count.contains(a)) {
				try {
					count.wait();
				} catch (InterruptedException e) {
				}
				System.out.println(a);
				count.remove(a);
				return;
			}
		}

	}

- Ajay Kumar April 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class ThreadQues {
	public static void main(String[] args) {
		Resource r = new Resource();
		new Thread(new RunnableTarget(r), "T1").start();
		new Thread(new RunnableTarget(r), "T2").start();
		new Thread(new RunnableTarget(r), "T3").start();
	}

	static class Resource {
		int item;
		int prev;

		public synchronized void addAndPrint() {
			prev=item;
			item++;
			System.out.println(Thread.currentThread().getName() + " added 1 to prev value [ "+prev +" ] so update value is [ " + item+"]");
		}

	}

	static class RunnableTarget implements Runnable {
		Resource r;

		public RunnableTarget(Resource r) {
			super();
			this.r = r;
		}

		@Override
		public void run() {
			while (true) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				r.addAndPrint();
			}

		}

	}

}

- Manoj July 17, 2013 | 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