Morgan Stanley Interview | Java | All rounds | April 2021 | Mumbai

Round 1:

1) What is Executor service
2) What is type erasure
3) What is callable interface
4) How to create thread
5) What are other way to create thread - using runnable.
6) Difference between runnable and thread. When to use runnable and when to use thread class to creat thread
7) Explain what is factory pattern
8) when to use arraylist and linkedlist with real world example
9) what is volatile keyword and what is atomic
10) Sort ArrayList<Employee> based on email addr: I told her the approach using compare(), she asked how to do that with comparator
11) Explain garbage collection concept
12) which method to call to explicitly call GC - System.GC()
13) Anyother way to call GC externally and cleanup
14) where to write code to get executed before JVM crash
15) How to Store employee as object inside hashmap
16) can hashmap allow storing object as key (Look in depth for what if employee is immutable object)
17) For example store Employee obj as a key,how to do that. How will it behave.
18) How to write code to store employee obj(sortinng based on email addr), i told using lambda and compareTo method.    She asked how to do it with comparator and comparable. 
19) Diff between comparator and comparable
20) internal working of hashmap
21) internal working of treemap difference between them
22) why do u use treemap and why hashmap
23) what is checked exception and what you can do with it
24) other way to throw exception without using try catch

Round 2:

- When u try to insert immutable object in map. Is it allowed ? How about mutable objects inside list ?
- difference between inserting immutable object and immutable object in map.
- what all challenges you will face while inserting mutable object as key in hashmap.
- For example You have Map<Student, String>, what if student is mutable
- how to create immutable object. i said all 6 Steps
- What is deep copy. How to perform deep copy for an object Student havong id, name, markSheet as map
- which all places we have to perform deep cloning(Exact classes)
- What all are the conding best practices to follow while facing heap space/permgem space (out of memory error): 
	- put logs
	- handfle proper null condition and isempty
	- recursion breaking condition take care
	- garbage collection

- When you create List/Object, how can you handle memory there. 
- Garbage collector 

- How to call GC and what to call (Finalize())
- Have you worked with multithreading
- Have you worked with ExecutorService. Explain Executorervice

Design question:

You have multiple upstream system and all upstream system is providing data from different sources. For Eg: api, file and DB
And you are getting fxrate, eqRate, MFPrice data and all. Designa system that can read through this different source systems regardless of where you are going to put that data./what u r going to do with that data.

Question on my below Designn
- When r u going to call processData method.
Ans: depends on upon if you want to get it run on scheduled time or immediate. Here say FxTrade completes processing at 5 PM and Trade team completes processing at 7. 
-How can you check, through polling/ autosys jobs or some kind of filewatcher. You can place call to your processData() method there.
  
  My DesignAnswers:
  My answers:
Public class SourceSystem {
	private String type;   //api, file, db
	processData();
}

Class ApiSystem extends SourceSystem  {
	private String tspApiUrl;
	private String odsApiUrl;

	@Override
	ProcessData()  {
        //jsonParser code
	}
}

Class FileSystem extends SourceSystem  {
	private String fileId,
	private String name;
	private String url;

	//getter and setter
       //constructors 
     
	@Override
	ProcessData()  {
		//csvFileReader based code
	}
}

Class DatabseSystem extends SourceSystem  {
	private String datasource;
	private String username;
       //password
	   
	//getter setter
      databseSystem(String ds, String name, String pwd){
		//initialize all this property directly or by reading from system property
        this.dataSource = ds;
		this.username = name;
  		//etc 
	}
}

Class SourceReader {
	
	public static SourceSystem getDataSource(String type) {
		if(type.equalsIgnoreCase(“api”) 
			return new ApiSystem();
		else if(type.equalsIgnoreCase(“file”) 
			return new FileSystem();
		else if(type.equalsIgnoreCase(“api”) 
			return new DatabaseSystem();	
	}
}

@RestController
Public class MsRiskCal {
	
	@PostMapping(/api/readData/{SourceType})
         public void getSource(@PathVariable(“SourceType) String type)  {
		SourceSystem s = SourceReader.getDataSource(type);
		s.processData();
	}
}

Round 3:


Q1:) WAP to return only once instance of each class. Second it should throw exception. 
Solution: Make use of "getClass()" or "instance Of" keyword and check reference on the same.

Class A {

      Static int aCount = 0;
     A() {
	  // if(aCount== 0)
		     //aCount++;
        //else 
			//throw new Exception();		
     }
}

Class B extends A {
       //Static int bCount = 0;
     B() {
		
     }
}

Class C extends B {

}

main() {
	A a1 = new A(); //works fine
	A a2 = new A();   //throw Exception

	B b1 = new B(); //works fine
	B b2 = new B(); //throw exception

	C c1 = new C();
	C c2 = new C(); //throw exception
}

Q2:) Find upper level hierarchy and lower level hierarchy using java and SQL.
For example output here:
uper level for ASSOC should return: (VP, ED, MD)
Lower level for VP should return:(ASSoC, ANALYST)

Employee {
	int id;
	String name;
	int mgrId;
}

Owner, 
MD, 
ED, 
VP, 
ASSoC, 
ANALYST

Ans: By placing Employee in Map<Id, mgrId>: for upper level
		By placing Employee in Map<mgrId, Id>: For lower level and iterate like below
		
emplList -> Employee(id, name, mgrId)
Iterator<> it = map.entrySet().iterator()
}

SQL Answer(Correct one):
Select id, name, mgrId from Employee e
Connect by prior e.id = e.mgrId;

Select e.name, m.name from Employee e
Employee m
Where e.id = m.mgrId;   

Round 4: With Hiring manager

  • Project specific discussion.
  • Coding:
    you Have one class having two methods.
M1() // is static synchronized
m2() // is synchronized
create two threads T1 and T2.
call T1.m1() and T2.m2(). 

Will these threads have to wait for each other?
which level of locking both methods provides and how.

What if we have only one thread and access m2() more than once. will it cause deadlock ?

t2.m2()

M2 {
   m2
}
  • Diff between put and post. Basically all methods
Comments (5)