## IBM coding question

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

The below code will alternate. This is just something to start with.

void recur(int **arr, int N, int I, int J) {
if(N==0) return;
else if(N==1) { arr[I][J]=0; }
else {
int start = N*N, i=I, j=J;
for(; j < J+N ; j++)            { arr[i][j] = (--start);  } //cout<<"arr["<<i<<"]["<<j<<"]="<<arr[i][j]<<" "; } cout<<endl;
for(i=i+1,j=j-1 ; i < I+N; i++) { arr[i][j] = (--start);  } //cout<<"arr["<<i<<"]["<<j<<"]="<<arr[i][j]<<" "; } cout<<endl;
for(j=j-1,i=i-1 ; j > J-1; j--) { arr[i][j] = (--start);  } //cout<<"arr["<<i<<"]["<<j<<"]="<<arr[i][j]<<" "; } cout<<endl;
for(i=i-1,j=j+1 ; i > I; i--)   { arr[i][j] = (--start);  } //cout<<"arr["<<i<<"]["<<j<<"]="<<arr[i][j]<<" "; } cout<<endl;
recur(arr, N-2, I+1, J+1);
}
}

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

Usage : recur(arr, N, 0, 0)

void recur(int **arr, int N, int I, int J) {
if(N==0) return;
else if(N==1) { arr[I][J]=0; }
else {
int start = N*N, i=I, j=J;
for(; j < J+N ; j++)            { arr[i][j] = (--start);  }
for(i=i+1,j=j-1 ; i < I+N; i++) { arr[i][j] = (--start);  }
for(j=j-1,i=i-1 ; j > J-1; j--) { arr[i][j] = (--start);  }
for(i=i-1,j=j+1 ; i > I; i--)   { arr[i][j] = (--start);  }
recur(arr, N-2, I+1, J+1);
}
}

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.