## Oracle Interview Question

Senior Software Development Engineers**Country:**India

**Interview Type:**In-Person

class Main{

static void spiralPrint(int m, int n, int a[][]) {

int i, k = 0, l = 0;

/* k - starting row index

m - ending row index

l - starting column index

n - ending column index

i - iterator

*/

while (k < m && l < n) {

for (i = l; i < n; ++i) {

System.out.print(a[k][i]+" ");

}

k++;

for (i = k; i < m; ++i) {

System.out.print(a[i][n-1]+" ");

}

n--;

if ( k < m) {

for (i = n-1; i >= l; --i) {

System.out.print(a[m-1][i]+" ");

}

m--;

}

if (l < n) {

for (i = m-1; i >= k; --i) {

System.out.print(a[i][l]+" ");

}

l++;

}

}

}

public static void main (String[] args) {

int R = 3;

int C = 6;

int a[][] = { {1, 2, 3, 4, 5, 6},

{7, 8, 9, 10, 11, 12},

{13, 14, 15, 16, 17, 18}

};

spiralPrint(R,C,a);

}

}

```
public class Demo {
public static void main(String args[]) {
char[][] matrix = {{'a','b','c','d'},{'e','f','g','h'},{'i','j','k','l'},{'m','n','o','p'}};
int rowLen=matrix.length;
int colLen=matrix[0].length;
int mod=0;
int start=0;
int end=colLen;
int incr=1;
for(int r=0;r<rowLen;r++) {
mod=r%2;
if(mod!=0) {
start=colLen-1;
end=0-1;
incr=-1;
}
else{
start=0;
end=colLen;
incr=1;
}
for(int j=start;mod==0?j<end:j>end;j=j+incr) {
System.out.println(matrix[r][j]);
}
}
}
}
```

- cvishwakarma July 18, 2018