TCS Codevita Previous Year Question & Answers (Solved Problems) | TCS CodeVita Previous Year Coding Questions

TCS Codevita Previous Year Question & Answers | TCS CodeVita Previous Year Coding Questions | TCS Codevita Previous Papers With Answers | TCS Codevita Pre-Qualifier Round Questions | TCS CodeVita Previous Year Coding Questions & Answers 

TCS Codevita Previous Year Question & Answers 2024: The coding test is medium complexity, meaning that while passing it is not impossible, it is also not very simple. If you practiced some of the code questions from the previous year’s examination, you would undoubtedly have an advantage in the upcoming TCS Codevita exam. Let’s look at a few test patterns, TCS Codevita MCQ Questions and Answers/ TCS Codevita MCQ Questions in Python, C, C++, Java, and Python code problems and their fixes. In the below section, you can find the TCS Codevita Exam Questions and Answers/ TCS Codevita Coding Questions & Answers 2023

Previous recruiting campaigns’ coding examinations placed a strong focus on arrays and other fundamental programming principles. One can easily pass the first round and even the ones after that if they prepare adequately. You must select a language that you are comfortable with, such as C, C++, Java, or even Python, in order to complete the coding tasks. More details like TCS Codevita Coding Questions and Answers, TCS Codevita Solved Questions & Answers, TCS Codevita Zone Wise Questions & Answers, TCS Codevita Questions & Answers, etc. available on its website

TCS Codevita Previous Year Question With Answers 2024

Java Program for Football League Problem

Football League Table Statement : All major football leagues have big league tables. Whenever a new match is played, the league table is updated to show the current rankings (based on Scores, Goals For (GF), Goals Against (GA)). Given the results of a few matches among teams, write a program to print all the names of the teams in ascending order (Leader at the top and Laggard at the bottom) based on their rankings.

Rules:
A win results in 2 points, a draw results in 1 point an
d a loss is worth 0 points. The team with the most goals in a match wins the match. Goal Difference (GD) is calculated as Goals For (GF) Goals Against (GA). Teams can play a maximum of two matches against each other Home and Away matches respectively.

The ranking is decided as follows: Team with maximum points is ranked 1 and minimum points is placed last Ties are broken as follows Teams with same points are ranked according to Goal Difference(GD).

If Goal Difference(GD) is the same, the team with higher Goals For is ranked ahead

If GF is same, the teams should be at the same rank but they should be printed in case-insensitive alphabetic according to the team names. More than 2 matches of same teams, should be considered as Invalid Input.

A team can’t play matches against itself, hence if team names are same for a given match, it should be considered Invalid Input

Input Format:
First line of input will contain number of teams (N) Second line contains names of the teams (Na) delimited by a whitespace character Third line contains number of matches (M) for which results are available Next M lines contain a match information tuple {T1 T2 S1 S2}, where tuple is comprised of the following information

  • T1 Name of the first team
  • T2 Name of the second team
  • S1 Goals scored by the first team
  • S2 Goals scored by the second team

Output Format:
Team names in order of their rankings, one team per line OR Print “Invalid Input” where appropriate.

Constraints:

0< N <=10,000 0<=S1,S2

Example:
Consider 5 teams Spain, England, France, Italy and Germany with the following fixtures:

  • Match 1: Spain vs. England (2-0) (Spain gets 2 points, England gets 0)
  • Match 2: England vs. France (1-1) (England gets 1 point, France gets 1)
  • Match 3: Spain vs. France (0-2) (Spain gets 0 points, France gets 2)

Table 1. Points Table after 3 matches

Team Name Matches Played Goals for Goals Against Goal Difference Points Ranking
Spain 2 3 2 1 2 2
England 2 1 4 -3 1 3
France 2 3 1 2 3 1
Italy 0 0 0 0 0 4
Germany 0 0 0 0 0 4
Since, Italy and Germany are tied for points, goals difference is checked. Both have same, so, Goals For is checked. Since both are same. Germany and Italy share the 4th rank. Since Germany appears alphabetically before Italy, Germany should be printed before Italy. Then the final result is: France Spain England Germany Italy
Sample Input 1:
5
Spain England France Italy Germany
Spain England 3 0
England France 1 1
Spain France 0 2
Sample Output 1:
France
Spain
England
Germany
Italy
Sample Input 2:
5
Spain England France Italy Germany
Spain England 3 0
England France 1 1
Spain Spain 0 2
Sample Output 2:
Invalid Input

PROGRAM

import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
              int n,m,i,j,k;
              int x=0;
              
              Scanner sc =new Scanner(System.in);
              System.out.println("\n\n");
              System.out.println("Enter no of teams");
              n = sc.nextInt();
              String[] z = new String[n];
              System.out.println("\n\n");
              System.out.println("Enter names of Teams:");
              for (i = 0; i < n; i++) {
                z[i]=sc.next();
            }
              System.out.println("\n\n");
              System.out.println("Enter no of matches");
                m=sc.nextInt();
                int[] pt = new int[n];
                int[] gf = new int[n];
                int[] ga = new int[n];
                int[] gd = new int[n];
                int[] mt = new int[n];
                String[] s = new String[n+n];
                String[][] a = new String[m][];
                System.out.println("\n\n");
                System.out.println("Enter details of matches (Team1 Team2 T1 T2goals)");
                for (i = 0; i < m; i++) {
                for (j = 0; j < 4; j++) {
                    a[i][j]=sc.next();
                }
                System.out.println();
            }
                
                for (i = 0; i < m; i++) {
                for (j = 0; j < 2; j++) {
                    k=0;
                    int temp=0;
                    int index=0;
                    while(((i-k)>=0)&&(i>0)) {
                        if(a[i][j].equals(s[i-k])) {
                            temp=1;
                            index=i-k;
                        }
                        k++;
                    }
                    if(temp==1) {
                        mt[index]+=1;
                        gf[index]+=Integer.parseInt(a[i][(j+2)]);
                        ga[index]+=Integer.parseInt(a[i][(3-j)]);
                        if((Integer.parseInt(a[i][j+2]))>(Integer.parseInt(a[i][3-j]))) {
                            pt[index]+=2;
                        }
                        else if(Integer.parseInt(a[i][j+2])==Integer.parseInt(a[i][3-j])) {
                            pt[index]+=1;
                        }
                        else {
                            pt[index]+=0;
                        }    
                    }
                    else {
                        s[x]=a[i][j];
                        mt[x]+=1;
                        gf[x]=Integer.parseInt(a[i][(j+2)]);
                        ga[x]=Integer.parseInt(a[i][(3-j)]);
                        if((Integer.parseInt(a[i][j+2]))>(Integer.parseInt(a[i][3-j]))) 
                            pt[x]=2;
                        else if(Integer.parseInt(a[i][j+2])==Integer.parseInt(a[i][3-j]))
                            pt[x]=1;
                        else
                            pt[x]=0;
                        x++;
                        
                    }
                }
            }
                int v=m;
                int[] p = new int[n];
                int b;
                int[] r = new int[n];
                
                for (i = 0; i < m; i++) {
                for (j = 0; j < (m-i-1); ++j) {
                    if(p[j]<p[j+1]) {
                        b=p[j];
                        p[j]=p[j+1];
                        p[j+1]=b;
                    }
                }
                int[] mp = new int[n];
                int[] goalf = new int[n];
                int[] goala = new int[n];
                String tn[] = new String[n+n];
                
                int[] g = null;
                for (i = 0; i < m; i++) {
                    for (j = 0; j < m; j++) {
                        if(pt[i]==p[j]) {
                            tn[j]=s[j];
                            mp[j]=mt[i];
                            goalf[j]=gf[i];
                            goala[j]=ga[i];
                            goala[j]=ga[i];
                            g[j]=gd[i];
                        }
                        
                    }
                }
                
                for (i = m; i < n; i++) {
                    mp[i]=0;
                    goalf[i]=0;
                    goala[i]=0;
                    g[i]=0;
                    p[i]=0;
                }
                
                for (i = 0; i < n; i++) {
                    for (j = 0; j < m; j++) {
                        if((z[i].equals(tn[j]))) {
                            z[i]="0";
                        }
                    }
                }
                for (i = 0; i < n; i++) {
                    if(z[i]!="0") {
                     tn[v]=z[i];
                     v++;
                }
            }
                for (i = 0; i < n; i++) {
                    if(p[i]==p[i+1]) {
                        if(g[i]>g[i+1]) {
                            r[i]=i+1;
                            i=i+1;
                        }
                        else if(g[i]<g[i+1]) {
                            r[i+1]=i+1;
                            r[i]=i+2;
                            i=i+1;
                        }
                        else {
                            try {
                                int d = tn[i].compareToIgnoreCase(tn[i+1]);
                                if(d>0) {
                                    s[i]=tn[i];
                                    tn[i]=tn[i+1];
                                    tn[i+1]=s[i];
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            r[i]=i+1;
                            r[i+1]=i+1;
                            i=i+1;
                        }
                    }
                    else
                        r[i]=i+1;
                    }
                System.out.println("\n\n\n\n");
                System.out.println("\t\t\t"+"Football Points Table"+"\t\t");
                System.out.println("\n\n");
                System.out.println("Name"+"\t\t"+"Match"+"\t"+"GoalF"+"\t"+"GoalA"+"\t"+"GD"+"\t"+"Points"+"\t"+"Rank");
                for (i = 0;i < n; i++) {
                 System.out.println(tn[i]+"\t\t"+mp[i]+"\t"+goalf[i]+"\t"+goala[i]+"\t"+g[i]+"\t"+p[i]+"\t"+r[i]);
                } 
                
                
                }
         }
            
    }

Python program for Sorting Boxes Problem

The parcel section of the Head Post Office is in a mess. The parcels that need to be loaded to the vans have been lined up in a row in an arbitrary order of weights. The Head Post Master wants them to be sorted in the increasing order of the weights of the parcels, with one exception. He wants the heaviest (and presumably the most valuable) parcel kept nearest his office.

You and your friend try to sort these boxes and you decide to sort them by interchanging two boxes at a time. Such an interchange needs effort equal to the product of the weights of the two boxes.

The objective is to reposition the boxes as required with minimum effort.

Input Format:

  • The first line consists of two space-separated positive integers giving the number of boxes (N) and the position of the Head Post Masters office (k) where the heaviest box must be.
  • The second line consists of N space-separated positive integers giving the weights of the boxes. You may assume that no two weights are equal

Output Format:

  • The output is one line giving the total effort taken to get the boxes in sorted order, and the heaviest in position k.

Constraints:

 N<=50 and Weights <= 1000

Sample Input 1:
5 2
20 50 30 80 70
Sample Output 1:
3600

PROGRAM

#JAVA SOLUTION FOR THIS IS TOO LENGTHY YOU CAN USE THIS LOGIC SAME IN JAVA AS WELL

size,k = map(int,input().split())
parcel = list(map(int,input().split()))
effort = 2*parcel[k-1]*min(parcel) + max(parcel)*min(parcel)
print(effort)

Java program for Forest Fire Problem

Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the complete forest. Not a single tree is left.This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8 directions,North, South, East, West, North-East, North-West, South-East, and South-West.And it is given that the fire is spreading every minute in the given manner, i.e every tree is passing fire to its adjacent tree.Suppose that the forest layout is as follows where T denotes tree and W denotes water.

Your task is that given the location of the first tree that catches fire, determine how long would it take for the entire forest to be on fire. You may assume that the lay out of the forest is such that the whole forest will catch fire for sure and that there will be at least one tree in the forest

Input Format:

  • First line contains two integers, M, N, space separated, giving the size of the forest in terms of the number of rows and columns respectively.
  • The next line contains two integers X,Y, space separated, giving the coordinates of the first tree that catches the fire.
  • The next M lines, where ith line containing N characters each of which is either T or W, giving the position of the Tree and Water in the  ith row of the forest.

Output Format:

Single integer indicating the number of minutes taken for the entire forest to catch fire

Constrains:

  • 3 ≤ M ≤ 20
  • 3 ≤ N ≤ 20

Sample Input 1:

3 3
W T T
T W W
W T T
Sample Output 1:

5

Explanation:
In the second minute,tree at (1,2) catches fire,in the third minute,the tree at (2,1) catches fire,fourth minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches fire.
Sample Input 2:
6 6
1 6
W T T T T T
T W W W W W
W T T T T T
W W W W W T
T T T T T T
T W W W W W

Sample Output 2:

16

In a Conference ,attendees are invited for a dinner after the conference.The Co-ordinator,Sagar arranged around round tables for dinner and want to have an impactful seating experience for the attendees.Before finalizing the seating arrangement,he wants to analyze all the possible arrangements.These are R round tables and N attendees.In case where N is an exact multiple of R,the number of attendees must be exactly N//R,,If N is not an exact multiple of R, then the distribution of attendees must be as equal as possible.Please refer to the example section before for better understanding.
For example, R = 2 and N = 3
All possible seating arrangements are
(1,2) & (3)
(1,3) & (2)
(2,3) & (1)
Attendees are numbered from 1 to N.

Input Format:

  • The first line contains T denoting the number of test cases.
  • Each test case contains two space separated integers R and N, Where R denotes the number of round tables and N denotes the number of attendees.

Output Format:

Single Integer S denoting the number of possible unique arrangements.

Constraints:

  • 0 <= R <= 10(Integer)
  • 0 < N <= 20 (Integer)
Sample Input 1:
1
2 5
Sample Output 1:

10

Explanation:

R = 2, N = 5

(1,2,3) & (4,5)

(1,2,4) & (3,5)

(1,2,5) & (3,4)

(1,3,4) & (2,5)

(1,3,5) & (2,4)

(1,4,5) & (2,3)

(2,3,4) & (1,5)

(2,3,5) & (1,4)

(2,4,5) & (1,3)

(3,4,5) & (1,2)

Arrangements like

(1,2,3) & (4,5)

(2,1,3) & (4,5)

(2,3,1) & (4,5) etc.

But as it is a round table,all the above arrangements are same.

import java.util.*;
public class SeatingArrangement
Program
{
//To find the factorial of the given number
public static int fact(int n)
{
int y = 1;
if(n==0||n==1)
return y;
else if(n>1)
{
for(int a = n;a>1;a–)
y = y*a;
return y;
}
else
return -1;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Number of test case
int t = sc.nextInt();
for(int b = 1;b0&&d=0&&r<=20)
{
int out = 1;
//The combination formula (nCr = n!/r!*(n-r)!)
out = fact(d)/(fact(r)*fact(d-r));
System.out.println(out);
}
}
}
}
JOB ALERT ON INSTAGRAM FOLLOW NOW>>
JOB ALERT ON YOUR EMAIL DAILY SUBSCRIBE NOW>>

Keep an eye on Dailyrecruitment.in for the most up-to-date information about upcoming exams and results. Find out about official notices, Exam Patterns, Syllabus, Previous Question Papers, Expected Cut Off, Results, Merit List, Study Materials, and much more. Now is the time to bookmark in order to take advantage of even more amazing career opportunities.

Govt Jobs by Qualifications

Education & Vacancies Salary Apply Link
10th Pass Govt Jobs - 5,000 Vacancies Rs. 5,200 - 63,200 Apply Now
12th Pass Govt Jobs - 18,000+ Vacancies Rs. 5,200 - 92,300 Apply Now
ITI Pass Jobs - 3,500 Vacancies Rs. 5,200 - 35,000 Apply Now
Any Graduate Jobs - 19,100 Vacancies Rs. 5,200 - 92,300 Apply Now
Central Govt Jobs Rs. 5,200 - 17,000 Apply Now
Bank Jobs - 1,000 Vacancies Rs. 5,200 - 29,200 Apply Now
Diploma Jobs - 9,300 Vacancies Rs. 5,200 - 35,000 Apply Now
BTech/BE Jobs - 18,000 Vacancies Rs. 15,000 - 1,00,000 Apply Now
Data Entry Jobs - 1,300 Vacancies Rs. 5,200 - 29,200 Apply Now
Private Jobs Rs. 10,000 - 67,700 Apply Now