Zoho Interview Question for SDE1s


Country: United States
Interview Type: Written Test




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

void rotateMatrixByClockwise(int matrix[][], int m, int n) {

		int r = 0, c = 0;
		int pre, cur;

		while (r < m && c < n) {

			if (r + 1 == m || c + 1 == n)
				break;

			pre = matrix[r + 1][c];

			for (int i = c; i < n; i++) {
				cur = matrix[r][i];
				matrix[r][i] = pre;
				pre = cur;
			}
			r++;

			for (int i = r; i < m; i++) {
				cur = matrix[i][n - 1];
				matrix[i][n - 1] = pre;
				pre = cur;
			}
			n--;

			if (r < m) {
				for (int i = n - 1; i >= c; i--) {
					cur = matrix[m - 1][i];
					matrix[m - 1][i] = pre;
					pre = cur;
				}
			}
			m--;
			if (c < n) {
				for (int i = m - 1; i >= r; i--) {
					cur = matrix[i][c];
					matrix[i][c] = pre;
					pre = cur;
				}
			}
			c++;
		}
	}

- Raj June 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void rotateMatrix(int matrix [] [], int size){//size is the size of the matrix MxM 
    int [][] rotatedMatrix = new int [size][size];
    
    for(int colIndex = 0; colIndex<size; colIndex++){
        for(int rowIndex=0; rowIndex<size; rowIndex++){
            rotatedMatrix[colIndex][rowIndex] = matrix [(size-1)-rowIndex][colIndex];// not sure why i did it this way. 
        }
    }
    for(int x=0; x<size; x++){
        for(int y=0; y<size; y++){
            System.out.println(rotatedMatrix[x][y]);
        }
    }
}

- Ivan Gonzalez June 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[][] rotate(int[][] m)
{
	if(m==null||m.length==m[0].length)
	{
		return null;
	}
	for(int layer=0;layer<m.length/2;layer++)
	{
		int minRow=layer;
		int minCol=minRow;
		int maxRow=m.length-layer-1;
		int maxCol=maxRow;
		if (minRow==maxRow)
		{
			break;
		}
		int tmp=m[minRow][minCol];
		for(int i=minRow+1;i<=maxRow;i++)
		{
			m[i-1][minCol]=m[i][minCol];
		}
		for(int i=minCol+1;i<=maxCol;i++)
		{
			m[maxRow][i-1]=m[maxRow][i];
		}
		for(int i=maxRow-1;i>=minRow;i--)
		{
			m[i+1][maxCol]=m[i][maxCol];
		}
		for(int i=maxCol-1;i>minCol;i--)
		{
			m[minRow][i+1]=m[minRow][i];
		}
		m[minRow][minCol+1]=tmp;
	}
	return m;
	
}

//Time Complexity: O(n^2) where n is the number of rows/columns in the input matrix.

- divm01986 June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

#define MAX_ARRAY_SIZE 10

void rotateMatrix(int size, int(*arr)[MAX_ARRAY_SIZE], int(*arr_to)[MAX_ARRAY_SIZE]);
void getNextCoordination(int size_from, int size_to, int x, int y, int *x_to, int *y_to);
void printArray(int size, int(*arr)[MAX_ARRAY_SIZE]);

int main()
{
	int arr[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE] = { 0, };
	int arr_to[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE] = { 0, };

	for (int i = 3; i <= 5; i++)
	{
		int num = 1;
		for (int k = 0; k < i; k++)
			for (int l = 0; l < i;l++)
				arr[k][l] = num++;

		rotateMatrix(i, arr, arr_to);
		
		printArray(i, arr);
		printArray(i, arr_to);
	}
	return 0;
	
}

void rotateMatrix(int size, int(*arr)[MAX_ARRAY_SIZE], int(*arr_to)[MAX_ARRAY_SIZE])
{
	int size_from = 0;
	int size_to = size - 1;
	int x, y, x_to, y_to;

	while (size_from <= size_to)
	{
		x = size_from;
		y = size_from;

		if (size_from == size_to)
		{
			arr_to[x][y] = arr[x][y];
		}
		else
		{
			for (int i = 1; i <= (((size_to - size_from + 1) * 2) + (((size_to - size_from + 1) - 2) * 2)); i++)
			{
				getNextCoordination(size_from, size_to, x, y, &x_to, &y_to);
				arr_to[x_to][y_to] = arr[x][y];
				x = x_to;
				y = y_to;
			}
		}
		size_from++;
		size_to--;
	}

}

void getNextCoordination(int size_from, int size_to, int x, int y, int *x_to, int *y_to)
{
	if (x == size_from)
	{
		if (y < size_to)
		{
			*x_to = x;
			*y_to = y + 1;
		}
		else
		{
			*x_to = x + 1;
			*y_to = y;
		}
	}
	else if (x > size_from && x < size_to)
	{
		if (y == size_from)
		{
			*x_to = x - 1;
			*y_to = y;
		}
		else if (y == size_to)
		{
			*x_to = x + 1;
			*y_to = y;
		}
	}
	else if (x == size_to)
	{
		if (y > size_from)
		{
			*x_to = x;
			*y_to = y - 1;
		}
		else
		{
			*x_to = x - 1;
			*y_to = y;
		}
	}
}

void printArray(int size, int(*arr)[MAX_ARRAY_SIZE])
{
	printf("---------------------\n");
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			printf("%2d ", arr[i][j]);
		}
		printf("\n");
	}
}

- jihow June 26, 2016 | 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