Zoho Interview Question for Students


Country: India
Interview Type: Written Test




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

Traversing the matrix in spiral form and storing the elements in stack should do if extra space is allowed.

import java.util.Stack;

public class AntiSpiralMatrixDisplay {
	
	public static void main(String args[])	{
		
		int arr[][] = new int[][]{ {1,2,3}, {4,5,6} , {7,8,9}};
		int MAX_ROW = 2;
		int MAX_COL = 2;
		Stack<Integer> stack = new Stack<Integer>();
		
		for ( int i = 0, j = 0 ; i <= MAX_ROW && j <= MAX_COL; i++ , j++ , MAX_ROW-- , MAX_COL-- ){
			
			int l,k;
			l=i;k=j;
			for (  ; k <= MAX_COL ; k++)
				stack.push(arr[l][k]);
			
			for ( l = i+1 , k = MAX_COL  ; l <= MAX_ROW ; l++)	
				stack.push(arr[l][k]);
			
			for ( l = MAX_ROW , k = MAX_COL - 1 ; k >= j ; k--  )
				stack.push(arr[l][k]);
			
			for ( l = MAX_ROW - 1 , k = j ; l > i ; l--)
				stack.push(arr[l][k]);
			
			
		}
		
		printAntiSpiralMatrix(stack);
	}

	private static void printAntiSpiralMatrix(Stack<Integer> stack ) {
		// TODO Auto-generated method stub
		while (!stack.empty())
			System.out.print(stack.pop()+ " ");
	}
}

- Sibendu Dey July 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 5

void spiralPrint(int m, int n, int a[R][C])
{
	int i, k = 0, l = 0;
	/* k - starting row index
		m - ending row index
		l - starting column index
		n - ending column index
		i - iterator
	*/
	stack<int> stk;
	while (k <= m && l <= n)
	{
		/* Print the first row from the remaining rows */
		for (i = l; i <= n; ++i)
			stk.push(a[k][i]);
		k++;

		/* Print the last column from the remaining columns */
		for (i = k; i <= m; ++i)
				stk.push(a[i][n]);
		n--;

		/* Print the last row from the remaining rows */
		if ( k <= m)
		{
			for (i = n; i >= l; --i)
				stk.push(a[m][i]);
			m--;
		}

		/* Print the first column from the remaining columns */
		if (l <= n)
		{
			for (i = m; i >= k; --i)
				stk.push(a[i][l]);
			l++; 
		}	 
	}
	
	while(!stk.empty())
	{
		cout << stk.top() << " ";
		stk.pop();
	}
}

/* Driver program to test above functions */
int main()
{
	int mat[R][C] = 
	{
		{1,  2,  3,  4,  5},
		{6,  7,  8,  9,  10},
		{11, 12, 13, 14, 15},
		{16, 17, 18, 19, 20}
	};

	spiralPrint(R - 1, C - 1, mat);
	
	return 0;
}

- Aditya Goel July 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main(){
int a[3][3]={{1,2,3},
{4,5,6},
{7,8,9}
};
int i=1,j=1;
while(j<3){
cout<<" "<<a[i][j];
j++;
}
--j;
i++;
while(j>=0){
cout<<" "<<a[i][j];
j--;
}
i--;
j++;
while(i>=0){
cout<<" "<<a[i][j];
i--;
}
++i;
++j;
while(j<3){
cout<<" "<<a[i][j];
j++;
}
}

- Rohan July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main(){
int a[3][3]={{1,2,3},
{4,5,6},
{7,8,9}
};
int i=1,j=1;
while(j<3){
cout<<" "<<a[i][j];
j++;
}
--j;
i++;
while(j>=0){
cout<<" "<<a[i][j];
j--;
}
i--;
j++;
while(i>=0){
cout<<" "<<a[i][j];
i--;
}
++i;
++j;
while(j<3){
cout<<" "<<a[i][j];
j++;
}
}

- Rohan July 28, 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