## IBM Interview Question for Interns

Team: Development
Country: India
Interview Type: Written Test

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

``````void printSpiral(int size)
{
int i, j, k,middle,l;
int arr[size][size];
l = size*size - 1;
if(size%2 == 0)
{
for(i=size-1, j=0; i>0; i--, j++)
{
for(k=j; k<i; k++) arr[j][k] = l--;
for(k=j; k<i; k++) arr[k][i] = l--;
for(k=i; k>j; k--) arr[i][k] = l--;
for(k=i; k>j; k--) arr[k][j] = l--;
}
}
else
{
for(i=size-1, j=0; i>0; i--, j++)
{
for(k=i; k>j; k--) arr[i][k] = l--;
for(k=i; k>j; k--) arr[k][j] = l--;
for(k=j; k<i; k++) arr[j][k] = l--;
for(k=j; k<i; k++) arr[k][i] = l--;
}
}

middle = (size-1)/2;
if (size % 2 == 1) arr[middle][middle] = 0;

for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
}``````

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

if n is odd, start from (n-1, n-1) and set the element clock wisely from n^2 - 1 to zero in the order of top, right, bottom and left
if n is even, start from (0, 0) and set the element close wisely from n^2 - 1 to zero in the oder of bottom, left, top and right

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

``````void spiral(int n)
{
vector< vector<int> > v(n, vector<int>(n,0));

int next = n*n - 1;

for(int i = 0; i < n/2; i++)
{
int x = i;
int y = i;

for(y = i; y < n-i; y++)
v[x][y] = next--;

y--;

for(x = i+1; x < n-i; x++)
v[x][y] = next--;

x--;

for(y--; y >= i; y--)
v[x][y] = next--;

y++;

for(x--; x > i; x--)
v[x][y] = next--;
}

// a function that prints out the matrix
print(v);

}``````

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

``````//this program creates a spiral of any no. of elements
void main()
{
int **a;
int x,y,i,step,k,length,j,flag;

length=10;flag=1;
a=malloc(sizeof(int*)*length);
for(i=0;i<length;i++)
{a[i]=malloc(sizeof(int)*length);
}

x=(length)/2;
y=(length-1)/2;
k=0;
step=1;
a[x][y]=k;
while(1)
{
for(i=0;i<step;i++)//right move
{
a[x][++y]=++k;
}

for(i=0;i<step;i++)//up
{
a[--x][y]=++k;
}
step++;

for(i=0;i<step;i++)//left
{
if(y==0)
{ goto outer;
}
a[x][--y]=++k;
}

if(!flag)
{
break; }

for(i=0;i<step;i++)//down
{
a[++x][y]=++k;
}    step++;

}

outer:;//outer is the level

for(i=0;i<length;i++)
{for(j=0;j<length;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}

}``````

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

``````void printSpiral(int size) {
int arr[size][size];
int startIdx = (size+1)/2 - 1;
int count = 0;
arr[startIdx][startIdx] = count;
count++;
int currIdxX = startIdx;
int currIdxY = startIdx;

for (int i = 2; i <= size; i++) {
if (i % 2 == 0) {// even size
currIdxX++; //right
arr[currIdxX][currIdxY] = count;
count++;
for (int m = 0; m < i - 1; m++) {
currIdxY++; //up
arr[currIdxX][currIdxY] = count;
count++;
}
for (int m = 0; m < i - 1; m++) {
currIdxX--; //left
arr[currIdxX][currIdxY] = count;
count++;
}
}

else { // odd size
currIdxX--; //left
arr[currIdxX][currIdxY] = count;
count++;
for (int m = 0; m < i - 1; m++) {
currIdxY--; //down
arr[currIdxX][currIdxY] = count;
count++;
}
for (int m = 0; m < i - 1; m++) {
currIdxX++; //right
arr[currIdxX][currIdxY] = count;
count++;
}

}
}

//print spiral
for (int j = size - 1; j >= 0; j--) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}``````

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

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

``````int main(void)
{
int number,n,i=0,j=0,icount=0, jcount=0;
printf("Enter n : ");
scanf("%d",&n);
int arr[n][n];
number = n*n;
number -= 1;
while(number >= 0)
{
i = icount;
for(j=jcount;j<n-jcount;j++)
arr[i][j]= number--;
j--;
for(i=icount+1;i<n-icount;i++)
arr[i][j] = number--;
i--;
for(j=(j-1);j>=jcount;j--)
arr[i][j]=number--;
j++;
for(i=(n-icount-2);i>icount;i--)
arr[i][j]=number--;

icount++;
jcount++;
}
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf(" %d ", arr[i][j]);
}
return 0;
}``````

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

class SpiralArray
{
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}};
int n= arr[0].length;
for(int j=0,a=0,b=0;j<=(n+1)/2;j++,b++,n--)
{

for(int i=j;i<n;i++)
{
System.out.println(arr[j][i]);
a=i;
}

for(int i=j+1;i<n;i++)
System.out.println(arr[i][a]);

for(int i=a-1;i>=j;i--)
{
System.out.println(arr[a][i]);
b=i;
}

for(int i=a-1;i>=j+1;i--)
System.out.println(arr[i][b]);

} //1st for close
}
}

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.

### 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.