Morgan Stanley Interview Question for Java Developers


Country: India
Interview Type: Written Test




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

for(i in 0..n-1)
  for(j in i+1..n-1)
    swap(a[i][j], a[j][i])

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

#include<iostream>

using namespace std;
int check(int x,int y,int ar[],int pos)
{ int r;
    for(int t=2;t<pos+2;t=t+2)
    {
            if((ar[t]==x && ar[t+1]==y)||(ar[t]==y && ar[t+1]==x))
            {
            r=1;
            break;
            }
                         else
                         {
                              r=0;
                         }
                         
            }
            ar[pos]=x;
            ar[pos+1]=y;
            return r;
}
int main()
{
    int n,i,j,ar[40],arr[5][5],temp,flag=0;
    cout<<"Enter n"<<endl;
    cin>>n;
    cout<<"Enter elements"<<endl;
    for(i=0;i<n;i++)
    {
            for(j=0;j<n;j++)
            cin>>arr[i][j];
            }
    cout<<"You have entered the following array"<<endl;
    for(i=0;i<n;i++)
    {
            for(j=0;j<n;j++)
            cout<<arr[i][j]<<"   ";
            cout<<endl;
            }
    cout<<"Mirroring array"<<endl;
    for(i=0;i<n;i++)
    {
            for(j=0;j<n;j++)
            { int m;
                            if(i==j)
                            {
                            m=check(i,j,ar,flag);
                            flag=flag+2;
                            }
                            else
                            {
                             m=check(i,j,ar,flag);
                             flag=flag+2;
                             if(m==1)
                             continue;
                             else
                             {
                                 temp=arr[i][j];
                                 arr[i][j]=arr[j][i];
                                 arr[j][i]=temp;
                             }
                             
                            }   
                           
                            }
            
            }
            
            cout<<"Your array has become"<<endl;
    for(i=0;i<n;i++)
    {
            for(j=0;j<n;j++)
            cout<<arr[i][j]<<"   ";
            cout<<endl;
            }
            
            system("pause");
    
}

- mohit February 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printMirrorReflext(int[][]arr){

for(int i=0;i<arr.length;i++){
for(int j=i;j<arr.length;j++){
if(i!=j){
int temp = arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}

}

for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
System.out.print(" "+arr[i][j]);
}
System.out.println();
}
}

- jeetu345 February 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check my answer as well..

- Amit Petkar March 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Don't have to do a single swap just switch the direction you iterate on the matrix: instead of left and down do down and left.

- Atilla February 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i in 0..n-1)
  for(j in 0..n-1)
   b[i][j]=a[j][i]

- vijay.edella March 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

/*
* Given a Input Matrix of NXN . Print the matrix after mirroring on its primary diagonal
if Input is 1,2,3,4,5.6,7,8,9
Output is 1,4,7,2,5,8,3,6,9
This program is also called the transpose of a matrix
*/
package careercup;

import java.util.*;

public class ArrayDiagnol {
public static void main(String[] args) {
int rowSize, colSize,temp;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of matrix");
rowSize = scan.nextInt();
colSize = scan.nextInt();
if (rowSize == colSize) {
System.out.println("Row Size = " + rowSize);
System.out.println("Column Size = " + colSize);
} else
System.out
.println("OOPS!!! Wrong input. You must enter same vale for row and column size\n Run the program again to input the values");
int matrix[][] = new int[rowSize][colSize];
System.out.println("Enter " + rowSize * colSize + " elements");

for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
matrix[i][j] = scan.nextInt();
}
}
System.out.println("Array elements are:");
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
for(int i = 0;i<rowSize;i++){
for(int j=i+1;j<colSize;j++){

temp = matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;

}
}
System.out.println("Array elements are:");
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}

- Vinutha March 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Matrix_Mirroring {
    public static void displayMatrix(int[][] arr) {
        for (int i = 0; i < arr[0].length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println("");
        }
    }
    
    
    public static void matrixMirror(int [][]arr)
    {
        int temp = 0;
        for(int i=0; i< arr.length; i++)
        {
            for(int j=0; j<=i-1; j++)
            {
                temp = arr[i][j];
                arr[i][j] = arr[j][i];
                arr[j][i] = temp;
            }
        }
    }
    
    public static void main(String[] args) {
        int[][] arr = {{1, 2, 3,4}, {5, 6, 7,8}, {9, 10, 11,12},{13,14,15,16}};
        
        displayMatrix(arr);
        
        matrixMirror(arr);
        
        System.out.println("---");
        displayMatrix(arr);
        
    }
    
}

- Amit Petkar March 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MatrixMirror {
	private static int[][] scores = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
	private static void mirror() {
		for(int i=0; i< scores.length ; i++) {
			for(int j=0; j<scores.length ; j++) {
				System.out.print("\t");
				System.out.print(scores[j][i]);
			}
		}
	}
	public static void main(String[] args) {
	mirror();
	}

}

- Srum September 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void print(int[] array)
{
int N = Math.sqrt(array.length);
for(int i=0; i<N;i++)
for(int j=i; j<array.length;j+=N)
System.io.print(array[j]);
}

- Gourab March 15, 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