 # TCS Ninja Coding Questions

You need to master TCS Ninja coding questions so that you can clear the TCS Ninja test. To make your preparation easy, we are discussing previously asked TCS Ninja coding questions. After going through the below mentioned questions, you will be in a position to answer any kind of coding questions. In this section, there is only one question for 20 minutes. A choice has been given to code in the given 5 langauges i.e C/C++/Java/Perl/ Python 2.7
1. Given a matrix with 0’s and 1’s , you enter the matrix at cell (0,0) in left to right direction. whenever you encounter a 0 you retain in same direction , if you encounter a 1’s you have to change direction to right of current direction and change that 1 value to 0, you have to find out from which index you will leave the matrix at the end.
###### Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines . The first line of each test case contains two integers n and mdenoting the size of the matrix. Then in the next line are n*mspace separated values of the matrix.
###### Output:
For each test case print in a new line two integers separated by space denoting the index of the matrix from which you will leave the matrix at the end.
Sample code
#include <iostream>
using namespace std;
void nxtdir(int &cx,int &cy)
{
if(cx==0 && cy==1)
{
cx=1;cy=0;return;
}
if(cx==-1 && cy==0)
{
cx=0;cy=1;return;
}
if(cx==0 && cy==-1)
{
cx=-1;cy=0;return;
}
if(cx==1 && cy==0)
{
cx=0;cy=-1;return;
}
return;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int m,n;
cin>>m>>n;
int a[m][n];
int cx=0,cy=1;
for(int j=0;j<m;j++)
{
for(int k=0;k<n;k++)
cin>>a[j][k];
}
int i1=0,i2=0;
while(i1<m && i2<n)
{
if(a[i1][i2]==0)
{
//cout<<"in "<<i1<<" "<<i2<<endl;
i1=i1+cx;
i2=i2+cy;
// cout<<"fn "<<i1<<" "<<i2<<endl;
// cout<<"cx "<<cx<<" "<<cy<<endl;
if(i1<0 || i1>=m || i2<0 || i2>=n)
{i1=i1-cx;i2=i2-cy;break;}
}
else
{
a[i1][i2]=0;
//cout<<"in1 "<<i1<<" "<<i2<<endl;
nxtdir(cx,cy);
i1=i1+cx;
i2=i2+cy;
//cout<<"fn1 "<<i1<<" "<<i2<<endl;
//cout<<"cx1 "<<cx<<" "<<cy<<endl;
if(i1<0 || i1>=m || i2<0 || i2>=n)
{i1=i1-cx;i2=i2-cy;break;}
}
}
cout<<i1<<" "<<i2<<endl;
}
return 0;
}
Q2. Given a value V, if we want to make change for V cents,and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins,what is the minimum number of coins to make the change?(
###### Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is V and N,V is the value of cents and N is the number of coins.
The second line of each test case contains N input C[i],value of available coins.
###### Output:
Print the minimum number of coins to make the change, if not possible print -1.
Sample code
#include <bits/stdc++.h>
using namespace std;
void getMin(int *a,int sum,int n,int index,int &min,int count)
{
if(sum == 0)
{
if(count < min)
{
min = count;
}
}
if(index>=n)
return;
if(a[index] <=sum)
{
getMin(a,sum-a[index],n,index,min,count+1);
getMin(a,sum,n,index+1,min,count);
}
else
{
getMin(a,sum,n,index+1,min,count);
}
}
int minn(int a,int b)
{
return a<b?a:b;
}
int main() {
//code
int c;
cin>>c;
for(int t=0;t<c;t++)
{
int v,n;
cin>>v>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
// int min = INT_MAX;
// getMin(a,v,n,0,min,0);
// cout<<min<<endl;
int dp[n+1][v+1] = {0};
for(int i=0;i<=n;i++)
dp[i] = 0;
for(int i=1;i<=v;i++)
dp[i] = INT_MAX;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=v;j++)
{
//dp[i][j] = INT_MAX;
if(a[i-1] <= j)
{
if(dp[i][j-a[i-1]]!= INT_MAX)
dp[i][j] = minn(dp[i][j-a[i-1]]+1,dp[i-1][j]);
else
dp[i][j] = dp[i-1][j];
}
else
{
dp[i][j] = dp[i-1][j];
}
}
}
cout<<(dp[n][v]==INT_MAX?-1:dp[n][v])<<endl;
}
return 0;
}