EMC Interview Question for Consultants

Team: VDirector
Country: United States
Interview Type: Phone Interview

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

int level = 0;
int x = -1;
int y = 0;
int tempX = 0;
int tempY = 0;
for (int k = 0; k < (n * 2) - 1; k++)
{
if (k >= n)
{
y++;
level--;
}
else
{
x++;
level++;
}

tempX = x;
tempY = y;
while (tempX != -1 && tempY != x+1)
{
Console.Write(A[tempX, tempY] + " ");
tempX--;
tempY++;
}
Console.WriteLine();

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

we would need the sum total of the size of the array which will help us in finding our the number of times we need to run the loop

``````int i=0,j=0,t=0;
boolean flag = false;
for (int c=0; c <= 9 ; c++){    //9 is addition of  i and j of 2D array
i = 0; j = t;
//System.out.pritln(c%5);
for (int k = 0; k <= t; k++){
System.out.print(a[i++][j--]);
}
System.out.print("\n");
if (t < 4 && flag == false)
t++;
else
{
t--;
flag = true;
}
}``````

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

But I guess your program does not meet the demanded output.So please refactor them more to print the demanded output.

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

``````public static void printMatrix(int[][] array)
{
if(array == null)
{
return ;
}
int rowCount = array.length;
int columnCount = array[1].length;
for(int i=0 ;i<rowCount ; i++)
{
int k =0;
int v = i;
while(v!= -1 && k <= i)
{
System.out.print(array[k][v]);
v--;
k++;
}
System.out.println();
}

for(int i = 1 ; i< columnCount ; i++)
{
int k = i;
int v = rowCount-1 ;
while(k<rowCount)
{
System.out.print(array[k][v]);
k++;
v--;
}
System.out.println();
}
}``````

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

before I posted the question I figured out the solution already.Wanted to see the response.

``````package com.sf.algorithmsStrcutures.binarySearch;

public class printBiMatrix {
static int a[][] = {	{1 ,2, 3, 4},
{5, 4, 3, 5},
{6, 5, 9, 8},
{9, 8, 7, 6}

};
static int factorial(int n){
int factorial = 1 ;
int max = n;
for(int index = max;index>0;index-- ){
factorial = factorial * index;
}
return factorial;

}

/**
* @param args
*/
public static void main(String[] args) {
int size = a.length;

//int numberOfChances = factorial(size);

for(int index=0;index < 2*size;index++){
for(int i=0;i < size;i++){
for(int j=0;j< size;j++){
if((i + j) == index){
System.out.print(a[i][j]);
}
}
}
System.out.println();
}

}

}
//Increase the bimatrix array into any size like 5*5 or 6*6 or 7*7 or 8*8``````

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

I'm trying to understand why people aren't surrounding their code with braces (as described immediately above the comment box) to preserve line breaks and offer code highlighting.

Did you not see these instructions? Did you not care? Did you not understand them?

I'd like to get people using this more widely, and I don't understand why they're not.

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

i dnt see the use of factorial in your code.

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

``````void test(int* array, int n)
{
int j = 0;
int i;
int index = 0;

for (index = 0 ; index < (n-1)*2 +1 ; ++index)
{
for (j = index ; j >= 0; j--)
{
i = index - j;
if (j < n && i < n)
{
printf("%d",array[i*n + j]);
}
}

printf("\n");
}
}``````

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

Try this code:

``````class PrintArray
{
public static void main(String[] args)
{
int[][]  arr=	{
{1 ,2, 3, 4,6},
{5, 4, 3, 5,7},
{6, 5, 9, 8,9},
{9, 8, 7, 6,1},
{1, 8, 3, 6,2}
};
for (int i = 0 ; i < arr.length ; i++)
{
int temp = i;
for ( int j=0; j <= i; j++ )
{
System.out.print(arr[j][temp--]);
}
System.out.print("\n");
}
for (int i = 1 ; i < arr.length ; i++)
{
int temp = i;
for (int j = arr[i].length - 1; j >= i; j--)
{
System.out.print(arr[temp++][j]);
}
System.out.print("\n");
}
}
}``````

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

``````public void print(int[][] array) {
int size = array.length;
for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) {
System.out.println(array[j][i-j]);
}
System.out.prinln("\n");
}
for (int i = 1; i < size; i++) {
for (int j = i; j <  size; j++) {
System.out.println(array[j][size - j]);
}
System.out.println("\n");
}
}``````

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

1
25
346
4359
65981
7878
878
78
8

The expected output is
1
25
346
4359
65981
7878
963
16
2

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

``````private static void PrintDiagonal(int[][] input)
{
int totalLines = input.Length * 2 - 1;
for (int line = 0; line < totalLines; line++)
{
Console.WriteLine();
int row = line >= input.Length ? input.Length - 1 : line;
for (int column = line - row; row < input.Length && row >= 0 && column >= 0 && column < input.Length; row--, column++)
{
Console.Write(" " + input[column][row]);
}
}
}``````

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

private static void printDiagonal(int[][] a, int m) {
int i,j;
int var_i,var_j;
for(j = 0; j < m; j++){
var_i = 0;
var_j = j;
do{
System.out.print(a[var_i][var_j]);
var_i++;
var_j--;
}while(var_i <= j);
System.out.println();
}

for(i = 1; i < m;i++){
var_i = i;
var_j = m-1;
do{
System.out.print(a[var_i][var_j]);
var_i++;
var_j--;
}while( var_j >= i);
System.out.println();
}
}

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

#define R 5
#define C 5
void printArr(int arr[R][C])
{
for(int i=0;i<C;i++)
{
int temp=0;
for(int j=i;j>=0;j--)
{
printf("%d",arr[temp][j]);
temp++;
}
printf("\n");
}
for(int i=1;i<C;i++)
{
int temp=i;
for(int j=R-1;j>=i;j--)
{
printf("%d",arr[temp][j]);
temp++;
}
printf("\n");
}
}
int main()
{
int arr[5][5]={{1 ,2, 3, 4,6},
{5, 4, 3, 5,7},
{6, 5, 9, 8,9},
{9, 8, 7, 6,1},
{1, 8, 3, 6,2}};
printArr(arr);
getchar();
return 0;
}

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

#include <stdio.h>

int ary[5][5] = { {1 ,2, 3, 4,6},
{5, 4, 3, 5,7},
{6, 5, 9, 8,9},
{9, 8, 7, 6,1},
{1, 8, 3, 6,2} };

int main() {

int x, y , z , w , k;
int change_y = 0;
int change_y_end = 0;

for(x = 0 ; x < 5; x++ ) {

for(y = change_y; y < 5 ; y++ ) {
z = x;
w = y;
for( k = y; k >= change_y_end; k-- ) {
printf("%d ", ary[z++][k]);
}
printf("\n");
}
if(y == 5){
change_y = 4;
}
change_y_end++;

}

return 0;
}

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

``````count=0;
for(i=0;i<5;i++)
{
for(x=0;x<=count;x++)
{
printf("%d",a[x][count-x]);
}
printf("\n");
count++;``````

}

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

``````int main()
{
int arr[5][5] ={ {1 ,2, 3, 4,6},
{5, 4, 3, 5,7},
{6, 5, 9, 8,9},
{9, 8, 7, 6,1},
{1, 8, 3, 6,2}
};
for(int i = 0; i < 5 ; i ++)
{
int temp = i;
for(int j = 0; j <= i ; j++)
cout<<arr[j][temp--];
cout<<"\n";

}
for(int i = 1; i < 5 ; i ++)
{
int temp = i;
for(int j = 4; j >= i ; j--)
cout<<arr[temp++][j];
cout<<"\n";

}
}``````

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

k

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

I think this is the simplest way...
int main()
{
int arr[5][5] ={ {1 ,2, 3, 4,6},
{5, 4, 3, 5,7},
{6, 5, 9, 8,9},
{9, 8, 7, 6,1},
{1, 8, 3, 6,2}
};
int temp=0;
While(temp<9)
{
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
if(i+j=temp)
Print(arr[i][j]);
if(i+j=8)
temp++;
}
print("/n");
}

}

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

Am i right???

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

i+j=8 line and i+j = temp are wrong.They should be == instead of .i,j are not declared.Except that,logic is correct

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

This code is more general. It simply along each left-hand diagonals and print the values.

``````void printDiagonalString(vector<vector<int> > &v){
int size = (int)v.size();
int len = size * 2 - 1;
int i, j;
for(int k = 0; k < len; k++){
if(k < size){
i = 0;
j = k;
}else{
i = k - size + 1;
j = size - 1;
}

while(i < size && j >= 0){
cout<<v[i++][j--];
}
cout<<endl;
}
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

this is very simple algorithem

``````void PrintMatrixSpecial(int [][] a)
{

int i =0,x,y;
for (i =0 ;i < size ;i++)
{

for (x=i ; x>=0;x--)
{
y = i-x;
printf("%d",a[x][y]);

}
printf("\n");
}

}``````

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

Its not working...better to unit test before posting.

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.

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.

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.

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.