“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
I don’t think I could write the correct code for this question in 15 minutes. The code below only show clockwise printing result. You are welcomed to optimize or generalize it.
Hi, This is what I think is Spiral Traversal - If you were given an array like: A B C D E 1 2 3 4 5 A B C D E 1 2 3 4 5 The output should be: ABCDE5E54321A1234DCB Key: All the elements of the array should be covered. Can you modify the code accordingly zanic? Thx!
This program works !!! I have hard-coded a 4×4 matrix called arr Run the program and see the results.. Anytime you want to use a different size matrix.. just change the row and col values that I initialized below..works for all cases that I tested..
int main () { int arr[][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16}};
int i = 0, j = 0, row = 4, col = 3, done_i = 0, done_j = 0; int count = row*col;
for (; i < row-done_i; i++) { for (; j < col-done_j; j++) { cout << arr[i][j] << ” “; cout.flush(); count–; }
for (j –, i++; i < row-done_i; i++) { cout << arr[i][j] << ” “; cout.flush(); count–; }
I don’t think I could write the correct code for this question in 15 minutes. The code below only show clockwise printing result. You are welcomed to optimize or generalize it.
#include <stdio.h>
char test[5][5] = { “ABCD”, “ABCD”, “ABCD”, “ABCD” };
void main() {
int x_count = 4, x_limit = 4, y_count = 4, y_limit = 4;
int pivol = 0, // x changed = 0, y changed = 1
i = 0,
j = 0,
prev_i = 1,
prev_j = 1,
temp;
while (x_limit != 0 && y_limit != 0) {
if (pivol == 0) {
if (x_count == x_limit) {
putchar(test[i][j]);
pivol = 1;
y_count = 1;
y_limit–;
temp = j;
j = prev_j;
prev_j = temp;
continue;
}
if (prev_i <= i) {
putchar(test[i][j]);
prev_i = i; i++; x_count++;
continue;
}
if (prev_i > i) {
putchar(test[i][j]);
prev_i = i; i–; x_count++;
}
}
if (pivol == 1) {
if (y_count == y_limit) {
putchar(test[i][j]);
pivol = 0;
x_count = 1;
x_limit–;
temp = i;
i = prev_i;
prev_i = temp;
continue;
}
if (prev_j <= j) {
putchar(test[i][j]);
prev_j = j; j++; y_count++;
continue;
}
if (prev_j > j) {
putchar(test[i][j]);
prev_j = j; j–; y_count++;
}
}
}
}
What do you mean by printing array in spiral fashion??
If you have 2D array as
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Does spiral printing mean ” 1 2 2 3 3 3 4 4 4… and so on? or printing 12345 23456 3456….
To ssanand:
You may probe what the interview intends to ask you.
Hi,
This is what I think is Spiral Traversal -
If you were given an array like:
A B C D E
1 2 3 4 5
A B C D E
1 2 3 4 5
The output should be: ABCDE5E54321A1234DCB
Key: All the elements of the array should be covered.
Can you modify the code accordingly zanic?
Thx!
This program works !!!
I have hard-coded a 4×4 matrix called arr
Run the program and see the results..
Anytime you want to use a different size matrix.. just change the row and col values that I initialized below..works for all cases that I tested..
int main ()
{
int arr[][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16}};
int i = 0, j = 0, row = 4, col = 3, done_i = 0, done_j = 0;
int count = row*col;
for (; i < row-done_i; i++)
{
for (; j < col-done_j; j++)
{
cout << arr[i][j] << ” “;
cout.flush(); count–;
}
for (j –, i++; i < row-done_i; i++)
{
cout << arr[i][j] << ” “;
cout.flush(); count–;
}
done_i++;
if (count <= 0)
break;
for (i–, j–; j >= done_j; j–)
{
cout << arr[i][j] << ” “;
cout.flush(); count–;
}
for (j++, i–; i >= done_i; i–)
{
cout << arr[i][j] << ” “;
cout.flush(); count–;
}
done_j++;
j++;
}
return 1;
}
Here is my universal solution. You can just copy and paste to run:
static void spiralRoute()
{
char a[7][7] = {
{’a', ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’},
{’h', ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’},
{’o', ‘p’, ‘q’, ‘r’, ’s’, ‘t’, ‘u’},
{’1′, ‘2′, ‘3′, ‘4′, ‘5′, ‘6′, ‘7′},
{’O', ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’},
{’H', ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’},
{’A', ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’},
};
char rightoutput[50] = “abcdefgnu7UNGFEDCBAHO1ohijklmt6TMLKJIP2pqrs5SRQ34″;
char myoutput[50];
/* DIRECTIONS:
0: towarding right;
1: towarding down;
2: towarding left;
3: towarding up;
*/
int direction = 0;
int up_ = 0, down_ = 6, right_ = 6, left_ = 0;
int i, counter = 0, bOver = 0;
while (!bOver) {
switch (direction)
{
case 0:
if (right_ < left_)
{
bOver = 1;
} else {
for (i = left_; i <= right_; i++)
{
myoutput[counter++] = a[up_][i];
}
up_++;
direction++;
}
break;
case 1:
if (down_ < up_)
{
bOver = 1;
} else {
for (i = up_; i <= down_; i++)
{
myoutput[counter++] = a[i][right_];
}
right_–;
direction++;
}
break;
case 2:
if (right_ < left_)
{
bOver = 1;
} else {
for (i = right_; i >= left_; i–)
{
myoutput[counter++] = a[down_][i];
}
down_–;
direction++;
}
break;
case 3:
if (down_ < up_)
{
bOver = 1;
} else {
for (i = down_; i >= up_; i–)
{
myoutput[counter++] = a[i][left_];
}
left_++;
direction = 0;
}
break;
}
}
myoutput[counter] = ‘