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.