Epic Systems Interview Question
Software DevelopersCountry: United States
Interview Type: Written Test
He means just spiral order, not matrix. So starting from upper left, print in a spiral, moving inward. If you look at the output to my program it should make sense.
void PrintSpiral(int* matrix, int w, int h)
{
int x = -1, y = 0, curLayers = 0;
while (1)
{
if (x++ == w - curLayers)
break;
for (; x < w - curLayers; x++)
printf("%d, ", matrix[y*w + x]);
x--;
if (y++ == h - curLayers)
break;
for (; y < h - curLayers; y++)
printf("%d, ", matrix[y*w + x]);
y--;
if (x-- == curLayers)
break;
for (; x >= curLayers; x--)
printf("%d, ", matrix[y*w + x]);
x++;
if (y-- == ++curLayers)
break;
for (; y >= curLayers; y--)
printf("%d, ", matrix[y*w + x]);
y++;
}
}
void main()
{
int mat[] = {1, 9, 21, 13,
3, 7, 8, -1,
13, 5, 2, 1,
5,-7, 3, 6};
PrintSpiral(mat, 4, 4);
getch();
}
Output:
1, 9, 21, 13, -1, 1, 6, 3, -7, 5, 13, 3, 7, 8, 2, 5,
public class Spiralprint {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] multi = new int[][]{
{ 1, 2, 3, 4},
{ 12, 13, 14, 5},
{ 11, 16, 15, 6},
{ 10, 9, 8, 7}
};
printSpiral(multi);
}
public static void printSpiral(int[][] arr)
{
int x = arr.length;
int y = arr[0].length;
int currminrow = 0,currmaxrow = x-1;
int currmincol = 0,currmaxcol = y-1;
int i;
int count=x*y;
while(count>0)
{
for(i=currminrow;i<=currmaxcol;i++)
{
count --;
System.out.print(arr[currminrow][i]+" ");
}
for(i=currminrow+1;i<=currmaxrow;i++)
{
count --;
System.out.print(arr[i][currmaxcol]+ " ");
}
for(i=currmaxcol-1;i>=currmincol;i--)
{
count --;
System.out.print(arr[currmaxrow][i]+ " ");
}
for(i=currmaxrow-1;i>currminrow;i--)
{
count --;
System.out.print(arr[i][currmincol]+ " ");
}
currminrow++;
currmincol++;
currmaxcol--;
currmaxrow--;
}
}
}
public class Spiralprint {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] multi = new int[][]{
{ 1, 2, 3, 4},
{ 12, 13, 14, 5},
{ 11, 16, 15, 6},
{ 10, 9, 8, 7}
};
printSpiral(multi);
}
public static void printSpiral(int[][] arr)
{
int x = arr.length;
int y = arr[0].length;
int currminrow = 0,currmaxrow = x-1;
int currmincol = 0,currmaxcol = y-1;
int i;
int count=x*y;
while(count>0)
{
for(i=currminrow;i<=currmaxcol;i++)
{
count --;
System.out.print(arr[currminrow][i]+" ");
}
for(i=currminrow+1;i<=currmaxrow;i++)
{
count --;
System.out.print(arr[i][currmaxcol]+ " ");
}
for(i=currmaxcol-1;i>=currmincol;i--)
{
count --;
System.out.print(arr[currmaxrow][i]+ " ");
}
for(i=currmaxrow-1;i>currminrow;i--)
{
count --;
System.out.print(arr[i][currmincol]+ " ");
}
currminrow++;
currmincol++;
currmaxcol--;
currmaxrow--;
}
}
}
public static void printSpiral(int[][] arr,int i,int j , int n ,int m){
if (i > n || j > m) return;
if (i == n || j == m ) {
System.out.print(arr[i][j]);
return;
}
for (int k = j ; k <= m ; k ++){
System.out.print(arr[i][k]+ ",");
}
for (int l = i + 1; l <= n ; l ++){
System.out.print(arr[l][m] + ",");
}
for (int e = m-1 ; e >= j ; e--){
System.out.print(arr[n][e]+ ",");
}
for (int f = n-1; f > i ; f-- ){
System.out.print(arr[f][j]+ ",");
}
printSpiral(arr,i+1 ,j+1, n-1, m-1);
}
public static void main(String[] args){
int[][] arr = {{1, 9, 21, 13},
{3, 7, 8, -1},
{13, 5, 2, 1},
{5,-7, 3, 6}};
printSpiral(arr,0,0,3,3);
//1,9,21,13,-1,1,6,3,-7,5,13,3,7,8,2,5
}
public static void printSpiral(int[][] arr,int i,int j , int n ,int m){
if (i > n || j > m) return;
if (i == n || j == m ) {
System.out.print(arr[i][j]);
return;
}
for (int k = j ; k <= m ; k ++){
System.out.print(arr[i][k]+ ",");
}
for (int l = i + 1; l <= n ; l ++){
System.out.print(arr[l][m] + ",");
}
for (int e = m-1 ; e >= j ; e--){
System.out.print(arr[n][e]+ ",");
}
for (int f = n-1; f > i ; f-- ){
System.out.print(arr[f][j]+ ",");
}
printSpiral(arr,i+1 ,j+1, n-1, m-1);
}
public static void main(String[] args){
int[][] arr = {{1, 9, 21, 13},
{3, 7, 8, -1},
{13, 5, 2, 1},
{5,-7, 3, 6}};
printSpiral(arr,0,0,3,3);
//1,9,21,13,-1,1,6,3,-7,5,13,3,7,8,2,5
}
a = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
[21,22,23,24,25]
]
row_start = 0
col_start = 0
row_length = len(a)-1
col_length = len(a)-1
while(row_start<= row_length):
i = row_start
j = col_start
while(i<row_length or j <=col_length):
if j <=col_length:
print(a[i][j], end = ", ")
j +=1
else:
i +=1
print(a[i][j-1], end = ", ")
j -=1
while(i>row_start+1 or j >col_start):
if j >col_start:
j -=1
print(a[i][j], end = ", ")
else:
i -=1
print(a[i][j], end = ", ")
row_start += 1
col_start +=1
row_length -=1
col_length -=1
public class SprialMatrix {
public static void main(String[] args) {
int[][] arr = new int[][] { { 1, 2, 3, 4 }, { 12, 13, 14, 5 },
{ 11, 16, 15, 6 }, { 10, 9, 8, 7 } };
for(int layer = 0; layer < arr.length/2; layer++) {
for(int t = layer; t < arr.length - layer - 1; t++) {
System.out.print(arr[layer][t] + " ");
}
for(int r = layer; r < arr.length - layer - 1; r++) {
System.out.print(arr[r][arr.length - layer - 1] + " ");
}
for(int b = arr.length - layer - 1; b > layer; b--) {
System.out.print(arr[arr.length - layer - 1][b] + " ");
}
for(int l = arr.length - layer - 1; l > layer; l--) {
System.out.print(arr[l][layer] + " ");
}
}
}
}
public class SprialMatrix {
public static void main(String[] args) {
int[][] arr = new int[][] { { 1, 2, 3, 4 }, { 12, 13, 14, 5 },
{ 11, 16, 15, 6 }, { 10, 9, 8, 7 } };
for(int layer = 0; layer < arr.length/2; layer++) {
for(int t = layer; t < arr.length - layer - 1; t++) {
System.out.print(arr[layer][t] + " ");
}
for(int r = layer; r < arr.length - layer - 1; r++) {
System.out.print(arr[r][arr.length - layer - 1] + " ");
}
for(int b = arr.length - layer - 1; b > layer; b--) {
System.out.print(arr[arr.length - layer - 1][b] + " ");
}
for(int l = arr.length - layer - 1; l > layer; l--) {
System.out.print(arr[l][layer] + " ");
}
}
}
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class P21 {
private static void printSpircal(char[][] matrix) {
int n = matrix.length;
for (int r = 0; r <= n / 2; r++) {
for (int j = r; j < n - r; j++) {
System.out.print(matrix[r][j] + " ");
}
for (int j = r + 1; j < n - r; j++) {
System.out.print(matrix[j][n - r - 1] + " ");
}
for (int j = n - r - 2; j >= r; j--) {
System.out.print(matrix[n - r - 1][j] + " ");
}
for (int j = n - r - 2; j > r; j--) {
System.out.print(matrix[j][r] + " ");
}
}
System.out.println();
}
public static void main(String[] args) {
char[][] matrix = { { 'a', 'b', 'c', '1' }, { 'd', 'e', 'f', '2' },
{ 'g', 'h', 'k', '3' }, { '4', '5', '6', '7' } };
printSpircal(matrix);
}
}
public class SpiralMatrixPrint {
public static List<Integer> printSpiral(int[][] matrix) {
int rowBegin = 0;
int rowEnd = matrix.length - 1;
int colBegin = 0;
int colEnd = matrix[0].length - 1;
List<Integer> result = new ArrayList<Integer>();
while (rowBegin <= rowEnd && colBegin <= colEnd) {
for (int j = colBegin; j <= colEnd; j++) {
result.add(matrix[rowBegin][j]);
}
rowBegin++;
for (int i = rowBegin; i <= rowEnd; i++) {
result.add(matrix[i][colEnd]);
}
colEnd--;
if (rowBegin <= rowEnd) {
for (int j = colEnd; j >= colBegin; j--) {
result.add(matrix[rowEnd][j]);
}
}
rowEnd--;
if (colBegin <= colEnd) {
for (int i = rowEnd; i >= rowBegin; i--) {
result.add(matrix[i][colBegin]);
}
}
colBegin++;
}
return result;
}
public static void main(String args[]) {
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12
// 13,14,15,16
// 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
System.out.println(printSpiral(matrix));
}
}
could you give more information?
- Anonymous March 05, 2015