TCS NQT Previous Year Coding Round Question Paper – 2

Question 1:

A parking lot in a mall has RxC number of parking spaces. Each parking psace will either be  empty(0) or full(1). The status (0/1) of a parking space is represented as the element of the matrix. The task is to find index of the row(R) in the parking lot that has the most of the parking spaces full(1).

Note :

RxC- Size of the matrix

Elements of the matrix M should be only 0 or 1.

Example 1:

Input :

3   -> Value of R(row)

3 -> value of C(column)

[0 1 0 1 1 0 1 1 1] -> Elements of the array M[R][C] where each element is separated by new line.

Output :

3  -> Row 3 has maximum number of 1’s

Example 2:

input :

4 -> Value of R(row)

3 -> Value of C(column)

[0 1 0 1 1 0 1 0 1 1 1 1] -> Elements of the array M[R][C]

Output :

4  -> Row 4 has maximum number of 1’s

Solution in C++:

``````#include <bits/stdc++.h>
using namespace std;

int main()
{
int r,c,a,sum=0,m=INT_MIN,in=0;
cin>>r>>c;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>a;
sum+=a;
}
if(sum>m)
{
m=sum;
in=i+1;
}
sum=0;
}
cout<<in;
}
``````

Solution in Python:

``````r=int(input())
c=int(input())
sum=0
m=0
id=0
for i in range(r):
for j in range(c):
sum+=int(input())
if sum>m:
m=sum
id=i+1
sum=0
print(id)
``````

Solution in Java:

``````import java.util.*;
class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int arr[][]=new int[row][col];

for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
arr[i][j]=sc.nextInt();

int max=0,count=0,index=0;
for(int i=0;i<row;i++)
{   	count=0;
for(int j=0;j<col;j++)
{
if(arr[i][j]==1)
count++;
}
if(count>max)
{
max=count;
index=i+1;
}
}
System.out.println(index);
}
}
``````

Question 2:

Given an integer array Array of size N the task is to find the count of elements whose value is greater than all of its prior elements.

Note : 1st element of the array should be considered in the count of the result.

For example,

Arr[]={7,4,8,2,9}

As 7 is the first element, it will consider in the result.

8 and 9 are also the elements that are greater than all of its previous elements.

Since total of  3 elements is present in the array that meets the condition.

Hence the output = 3.

Example 1:

Input

5 -> Value of N, represents size of Arr

7-> Value of Arr[0]

4 -> Value of Arr[1]

8-> Value of Arr[2]

2-> Value of Arr[3]

9-> Value of Arr[4]

Output :

3

Example 2:

5   -> Value of N, represents size of Arr

3  -> Value of Arr[0]

4 -> Value of Arr[1]

5 -> Value of Arr[2]

8 -> Value of Arr[3]

9 -> Value of Arr[4]

Output :

5

Constraints

1<=N<=20

1<=Arr[i]<=10000

Solution in Java:

``````import java.util.*;
class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();

int max=Integer.MIN_VALUE;
int count=0;
for(int i=0;i<n;i++)
{
if(arr[i]>max)
{
max=arr[i];
count++;
}
}
System.out.println(count);
}
}
``````

Solution In C++

``````#include <bits/stdc++.h>
using namespace std;

int main()
{
int n,c=0,a,m=INT_MIN;
cin>>n;
while(n--)
{
cin>>a;
if(a>m)
{
m=a;
c++;
}
}
cout<<c;
}
``````

Solution In Python

``````import sys
n=int(input())
c=0
m=-sys.maxsize-1
while n:
n-=1
a=int(input())
if a>m:
m=a
c+=1
print(c)
``````

Question 3:

Airport security officials have confiscated several item of the passengers at the security check point. All the items have been dumped into a huge box (array). Each item possesses a certain amount of risk[0,1,2]. Here, the risk severity of the items represent an array[] of N number of integer values. The task here is to sort the items based on their levels of risk in the array. The risk values range from 0 to 2.

Example :

Input :

7  -> Value of N

[1,0,2,0,1,0,2]-> Element of arr[0] to arr[N-1], while input each element is separated by new line.

Output :

0 0 0 1 1 2 2  -> Element after sorting based on risk severity

Example 2:

input : 10  -> Value of N

[2,1,0,2,1,0,0,1,2,0] -> Element of arr[0] to arr[N-1], while input each element is separated by a new line.

Output :

0 0 0 0 1 1 1 2 2 2  ->Elements after sorting based on risk severity.

Explanation:

In the above example, the input is an array of size N consisting of only 0’s, 1’s and 2s. The output is a sorted array from 0 to 2 based on risk severity.

Solution in Java:

``````import java.util.*;
class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();

int countZero=0,countOne=0,countTwo=0;
for(int i=0;i<n;i++)
{
if(arr[i]==0)
countZero++;
else if(arr[i]==1)
countOne++;
else if(arr[i]==2)
countTwo++;
}
int j=0;
while(countZero>0)
{
arr[j++]=0;
countZero--;
}
while(countOne>0)
{
arr[j++]=1;
countOne--;
}
while(countTwo>0)
{
arr[j++]=2;
countTwo--;
}

for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
}
``````