Morgan Stanley On-Campus Internship Questions and Solutions
Anonymous User
2385

Round1 : Aptitude ( 45 minnutes )
Round2 : Coding Questions ( 45 minutes )

The questions in Coding Round were ( 2 questions ) :

A company has N products and K different stocks. Every dealer has specified the number of products they wish to receive in a carton. 
The company gets specified amount of profit per carton from the dealer. 
The company can either provide a full carton of products to the dealer or none at all. Find the maximum profit the company can earn. 
Input :
N K
The number of products in a carton of Dealer i  ( amountList )
The profit the Company earns per carton from Dealer i  ( orderList )
Example:
68 4
21 23 54 65
51 64 75 87

Output:
179

Explanation:
1 carton from Dealer 1
2 cartons from Dealer 2
Total products = 21 + 23*2 =67
Total Profit = 51 + 64*2 = 179

The problem can be solved using 0/1 Knapsack technique.
Similar question : #416 https://leetcode.com/problems/partition-equal-subset-sum/

int maxProfit(int N, int K, vector<int>& amountList, vector<int>& orderList) {

	vector<int> dp(0, N+1);
	
	for (int i=0; i<=N; i++){
            for (int j=0; j<K; j++){
			     if (amountList[j] <= i)
				     dp[i] = max(dp[i], dp[i-amountList[j]] + orderList[j]);
            }
    }

	return dp[N];
}
A circuit consisting of Transistors, Resistors and Capacitors. Transistors act as switch, Resistors divide the voltage and Capacitors store the electrical
energy. The distance between the Transistor and the Resistor is D units. When the Transistors are switched on, the Currnt flows at a rate of R units. 
The rate of current decreases by 1 unit for each unit of distance it travels. There are N Capacitors in path of current flow and given its distance
from Transistor and the energy stored, find the minimum number of Capacitors needed for current to reach its destination, if not possible return -1.
The rate of energy increses by 1 unit for every unit of charge it receives from the capacitor.
Input :
N D R
Distance of Capacitor i from Transistor
Energy stored in Capacitor i
Example:
3 15 5
5 7 10
2 3 5

Output:
3

Explanation:
Every Capacitor needs to be activated to reach the destination

This question is very much similar to #871 https://leetcode.com/problems/minimum-number-of-refueling-stops/
Also thanks to @amsv_24 for pointing it out.
The above question can be solved by picking the best choice (most energy capacitor) greedily, so we can do it using a Priority Queue.
We need to find the minimum number of Capacitors needed for the cCurrent to travel distance D.

int minCapacitors( int D, int R, vector<int>& distance, vector<int>& current) {   
        int N = distance.size();
        vector<vector<int>> v(N);
    	for(int i=0;i<N;i++)
    	    v[i].push_back({ distance[i], current[i]});
        priority_queue<int> pq;
        int reach = R, ans = 0, i = 0;
        while(reach < D) {
            while(i < N && reach >= v[i][0])
                pq.push(v[i++][1]);
            if(pq.empty())
                return -1;
            reach += pq.top(); 
            pq.pop();
            ans++;
        }
        return ans;
}

Please Upvote! Do share your ideas, feedbacks and questions. Happy Learning :)

Comments (4)