Hot Topics
Morgan Stanley Solution
Technical Round
- Question 1
Difference between C++ and Java.
- Answer
C++ | Java |
1. C++ is a platform dependent language. | Java is a platform independent language. |
2. C++ supports multiple inheritance. | Java does not support multiple inheritance but it can be achieved using interfaces. |
3. C++ supports operator overloading. | Java does not support operator overloading. |
4. C++ supports go to statement. | Java does not support go to statement. |
5. We have both call by value and call by reference in C++. | Java does not support call by reference but supports call by value. |
- Question 2
Implementation of Map
- Answer
A Map
(also known as a Dictionary
or Hash Map
) is a data structure that stores key-value pairs, where each key maps to a unique value. Here is an implementation of a Map
in Python:
class Map:
def __init__(self):
self.data = {}
def put(self, key, value):
self.data[key] = value
def get(self, key):
return self.data.get(key)
def remove(self, key):
del self.data[key]
def keys(self):
return self.data.keys()
def values(self):
return self.data.values()
def items(self):
return self.data.items()
def is_empty(self):
return not bool(self.data)
def size(self):
return len(self.data)
In this implementation, we use a dictionary self.data
to store the key-value pairs. The put
method adds a new key-value pair to the map, the get
method returns the value associated with a given key, the remove
method removes a key-value pair from the map, the keys
method returns a list of all keys, the values
method returns a list of all values, the items
method returns a list of all key-value pairs as tuples, the is_empty
method returns True
if the map is empty and False
otherwise, and the size
method returns the number of key-value pairs in the map.
The time complexity of the put
, get
, and remove
operations is O(1) on average, as they use the dictionary data structure, which has average constant-time performance for these operations. The time complexity of the keys
, values
, and items
methods is O(n), where n is the number of key-value pairs in the map, as they need to return a list of all keys, values, or key-value pairs. The is_empty
and size
methods have a time complexity of O(1).
- Question 3
You have an array of int with different numbers. You have to sort it in a way that the all the even numbers should come in the starting and odd numbers in the end. You cannot use a temp array or extra array in your solution.
- Answer
Here is an in-place solution to sort an array of integers into even numbers followed by odd numbers, without using an extra array:
def sort_even_odd(arr):
left = 0
right = len(arr) - 1
while left < right:
while arr[left] % 2 == 0 and left < right:
left += 1
while arr[right] % 2 == 1 and left < right:
right -= 1
if left < right:
arr[left], arr[right] = arr[right], arr[left]
return arr
The idea behind this solution is to use two pointers, one starting from the beginning of the array (left
) and one starting from the end (right
). The left
pointer moves right until it finds an odd number, and the right
pointer moves left until it finds an even number. When both pointers have found an odd number and an even number, respectively, they are swapped. The loop continues until the pointers cross each other, meaning that all even numbers are now at the beginning of the array and all odd numbers are at the end.
The time complexity of this solution is O(n), where n is the number of elements in the array, as it visits each element at most twice, once with the left
pointer and once with the right
pointer.
- Question 4
How to find the most frequent number in an array?
- Answer
Here is one way to find the most frequent number in an array in Python:
def find_most_frequent(arr):
frequency = {}
for num in arr:
if num in frequency:
frequency[num] += 1
else:
frequency[num] = 1
max_frequency = 0
most_frequent = None
for num, count in frequency.items():
if count > max_frequency:
max_frequency = count
most_frequent = num
return most_frequent
This solution uses a dictionary (frequency
) to keep track of the frequency of each number in the array. The dictionary maps each number to its frequency, so if the same number appears multiple times, its frequency is updated in the dictionary. After looping through all the numbers in the array, the function then finds the number with the highest frequency by looping through the items in the dictionary and keeping track of the number with the maximum frequency.
The time complexity of this solution is O(n), where n is the number of elements in the array, as it visits each element once to update its frequency in the dictionary, and then visits each item in the dictionary once to find the most frequent number. The space complexity is O(n), as the frequency dictionary takes up O(n) space.
- Question 5
Questions about core java, multithreading, collection, GC, j2ee, spring, hibernate, JMS, webservice,SQL and so on. Broad, but not to deep since its first round.
- Answer
Sure! I’d be happy to help answer any questions you have about these topics. Please provide specific questions and I’ll do my best to provide answers.
- Question 6
If you have an array of numbers and a sum, how can you check if any two numbers in that array add up to that sum?
- Answer
#include<bits/stdc++.h>
using namespace std;
bool majorityElement(int arr[],int n,int target){
unordered_map<int,int> mp;
for(int i=0;i<n;i++){
if(mp.find(target-arr[i])!=mp.end()){
cout<<"True"<<endl;
return true;
}
mp[arr[i]]=i;
}
cout<<"False"<<endl;
return false;
}
int main() {
int n;
cout<<"Enter the size of the array you want to create"<<endl;
cin>>n;
int arr[n];
cout<<"Enter the elements in the array"<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
int target;
cout<<"Enter target: "<<endl;
cin>>target;
majorityElement(arr, n,target);
return 0;
}
/*
output:
Enter the size of the array you want to create 5
Enter the elements in the array 1 2 3 4 5
Enter target: 3
True
*/