TCS NQT Annual Sports Team Coding Question
We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:
For an annual sports event, the college is selecting members in a team for national level games. For one of the games, the team selection criterion is height. Candidates who wish to be part of that team for any game fill the registration form. So for Game A, from N number of registered candidates, only M number of candidates will be selected based on their heights. Height[] is an array of a float representing heights of registered candidates whose names are maintained in a string array candidates[] at the same index position as of Height[].
The problem is to identify the members selected for the team for game A. (display the names of selected candidates)
Inputs are as follows:
“`
1. Number of required players required to form a team:
a. <Integer>
2. Names of registered participants. Users can enter N number of names. Names should be string values.
3. Heights[] of candidates whose names are taken above maintaining the mapping with names and heights. The number of inputs of heights should be equal to the number of inputs of names.
4. Q or q should be pressed to stop taking input namesExample 1:
“`
Input values:
“`
8
Pratik
Joy
Nimesh
Nikhil
Suresh
Ramesh
Jitu
Ashish
Swayam
Harsh
Lara
Mitesh
Santosh
Raj
Q
5.6
5.4
5.5
5.5
5.6
5.4
5.7
5.9
6.1
6.2
6.5
6.8
7.1
6.8
“`
Output values:
“`
Santosh
Raj
Mitesh
Lara
Harsh
Swayam
Ashish
Jitu
“`
**Algorithm**
“`
1. Read team_strength
2. Declare and initialise Players array with {}.
3. While Forever, do,
Read name.
if name is ‘Q’ or name is ‘q’, do,
break
Append name to Players
4. Declare and initialise heights array with {}.
5. For i from 0 to length(Players) – 1 inclusive, do,
Read height
Append height to heights
6. For i from 0 to length(Players) – 1 inclusive, do,
For j from i to length(Players) – 1 inclusive, do,
if heights[j] >= heights[i], do,
swap(heights[j], heights[i])
swap(Players[j], Players[j])
7. For i from 0 to team_strength – 1 inclusive, do,
print Players[i]
Program in C
#include <stdio.h>
#include <string.h>
int main()
{
int team_strength, i = 0;
scanf("%d", &team_strength);
char players[team_strength * 2][20];
while (1)
{
scanf("%s", players[i]);
if (!strcmp(players[i], "Q") || !strcmp(players[i], "q"))
break;
i++;
}
float height[team_strength * 2];
int input_strength = i;
for ( i = 0 ; i < input_strength ; i++ )
scanf("%f", &height[i]);
char temp[20];
for ( i = 0 ; i < input_strength ; i++ )
{
for ( int j = i + 1 ; j < input_strength ; j++ )
{
if ( height[j] >= height[i] )
{
height[j] += height[i];
height[i] = height[j] - height[i];
height[j] -= height[i];
strcpy(temp, players[i]);
strcpy(players[i], players[j]);
strcpy(players[j], temp);
}
}
}
for(i = 0 ; i < team_strength ; i++)
printf("%s\n", players[i]);
return 0;
}
Program in C++
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::istream;
using std::swap;
int main () {
size_t team_size;
cin >> team_size;
vector <string> players;
string player;
cin.ignore();
do {
getline(cin, player);
players.push_back(player);
} while (!(player == "Q" || player == "q"));
players.pop_back();
if (players.size() < team_size)
{
cout << "Players insufficient";
return 0;
}
vector <float> heights;
float height;
for (size_t i = 0 ; i < players.size() ; i++)
{
cin >> height;
heights.push_back(height);
}
for (size_t i = 0 ; i < players.size() ; i++)
for (size_t j = i + 1 ; j < players.size() ; j++)
if ( heights.at(j) >= heights.at(i) )
{
swap ( heights.at(j), heights.at(i) );
swap ( players.at(j), players.at(i) );
}
for (size_t i = 0 ; i < team_size ; i++)
cout << players.at(i) << endl;
return 0;
}
Program in Java
import java.util.Scanner;
import java.util.ArrayList;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int team_strength = in.nextInt();
ArrayList<String> players = new ArrayList<String>();
String name;
while (true)
{
name = in.next();
if (name.toUpperCase().equals("Q"))
break;
players.add(name);
}
int input_strength = players.size();
float[] height = new float[input_strength];
for (int i = 0 ; i < input_strength ; i++)
height[i] = in.nextFloat();
for (int i = 0 ; i < input_strength ; i++)
{
for (int j = i + 1 ; j < input_strength ; j++)
{
if ( height[j] >= height[i] )
{
height[j] += height[i];
height[i] = height[j] - height[i];
height[j] -= height[i];
name = players.get(i);
players.set(i, players.get(j));
players.set(j, name);
}
}
}
for (int i = 0 ; i < team_strength ; i++)
System.out.println(players.get(i));
}
}
Program in Python
if __name__ == "__main__":
team_strength = int(input())
players = []
while True:
name = input()
if name.upper() == 'Q':
break
players.append(name)
height = []
for _ in range(len(players)):
height.append(float(input()))
for i in range (len(players)):
for j in range (i, len(players)):
if height[j] >= height[i]:
height[j], height[i] = height[i], height[j]
players[j], players[i] = players[i], players[j]
for i in range (team_strength):
print(players[i])
Pingback: TCS NQT CODING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS