List of TCS Codevita 2016 Problems asked

Here is the TCS codevita 2016 Questions. These questions are somewhat new and mind twisting. Each question takes lots of time to solve. Here are the questions.

TCS codevita 2016 Problem 1: Logic Pyramid

Identify the logic behind the series

6  28  66  120  190  276…..

The numbers in the series should be used to create a Pyramid. The base of the Pyramid will be the widest and will start converging towards the top where there will only be one element. Each successive layer will have one number less than that on the layer below it. The width of the Pyramid is specified by an input parameter N. In other words there will be N numbers on the bottom layer of the pyramid.

TCS codevita 2016 Problems

The Pyramid construction rules are as follows

  1. First number in the series should be at the top of the Pyramid
  2. Last N number of the series should be on the bottom-most layer of the Pyramid, with Nth number being the right-most number of this layer.
  3. Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed. Have a look at the examples below to get a pictorial understanding of what this rule actually means.

Example

If input is 2, output will be

00006
00028 00066

If input is 3, output will be

00006
00028 00066
00120 00190 00276

Formal input and output specifications are stated below

Input Format:

First line of input will contain number N that corresponds to the width of the bottom-most layer of the Pyramid

Output Format:

The Pyramid constructed out of numbers in the series as per stated construction rules

Constraints:

  • 0<N<=14

TCS codevita 2016 Problem 2: Min Product Array

The task is to find the minimum sum of Products of two arrays of the same size, given that k modifications are allowed on the first array. In each modification, one array element of the first array can either be increased or decreased by 2. 
Note- the product sum is Summation (A[i]*B[i]) for all i from 1 to n where n is the size of both arrays 
Input Format: 
  1. First line of the input contains n and k delimited by whitespace
  2. Second line contains the Array A (modifiable array) with its values delimited by spaces
  3. Third line contains the Array B (non-modifiable array) with its values delimited by spaces
Output Format: 
  1. Output the minimum sum of products of the two arrays 
Constraints:
  1. 1 = N = 10^5
  2. 0 = |A[i]|, |B[i]| = 10^5
  3. 0 = K = 10^9

Explanations:

Explanation for sample 1: 

Here total numbers are 3 and total modifications allowed are 5. So we modified A[2], which is -3 and increased it by 10 (as 5 modifications are allowed). Now final sum will be
(1 * -2) + (2 * 3) + (7 * -5)
-2 + 6 – 35
-31

-31 is our final answer.

Explanation for sample 2: 

Here total numbers are 5 and total modifications allowed are 3. So we modified A[1], which is 3 and decreased it by 6 (as 3 modifications are allowed).
Now final sum will be
(2 * 3) + (-3 * 4) + (4 * 2) + (5 * 3) + (4 * 2)
6 – 12 + 8 + 15 + 8
25

25 is our final answer.

TCS codevita 2016 Problem 3: Consecutive Prime Sum

Some prime numbers can be expressed as Sum of other consecutive prime numbers.
For example

5 = 2 + 3
17 = 2 + 3 + 5 + 7
41 = 2 + 3 + 5 + 7 + 11 + 13

Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.

Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.

Input Format:

First line contains a number N

Output Format:

Print the total number of all such prime numbers which are less than or equal to N.

Constraints:

  1. 2<N<=12,000,000,000
Sample:
TCS codevita 2016 Problem sample input output

TCS codevita 2016 Problem 4 : Bishop Moves

Background

A Chess board position is accurately captured by Forsyth-Edwards notation and is abbreviated as FEN. A FEN “record” defines a particular game position, all in one text line and using only the ASCII character set. A FEN record contains six fields. A complete description of the FEN format to represent Chess positions can be found at here.

For the purpose of this problem only consider first of the six fields of FEN. Before we describe the problem, let us look at how FEN maps to a board position. The following 5 images show board positions and its corresponding FEN representation.

Figure 1.

This board position depicts initial position before any side has made a move. 
In FEN format this board position is represented as

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w

Let’s say, White plays e4. Then the board position looks like shown below.

Figure 2.

This board position depicts the Chess board after White has played e4. 
In FEN format this board position is represented as
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b

Similarly, 3 more half-moves are depicted in following diagrams

Figure 3.


Figure 4.


Figure 5.

The FENs corresponding to Figure 3, 4 and 5 are represented as(in bold)

3. rnbqkbnr/pppp1ppp/8/4P3/4P3/8/PPPP1PPP/RNBQKBNR w
4. rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR b
5. rnbqkbnr/pppp1ppp/8/8/4Pp2/8/PPPP2PP/RNBQKBNR w


Wikipedia describes first field of FEN format as follows

Each rank is described, starting with rank 8 and ending with rank 1; within each rank, the contents of each square are described from file “a” through file “h”. Following the Standard Algebraic Notation (SAN), each piece is identified by a single letter taken from the standard English names (pawn = “P”, knight = “N”, bishop = “B”, rook = “R”, queen = “Q” and king = “K”).[1] White pieces are designated using upper-case letters (“PNBRQK”) while black pieces use lowercase (“pnbrqk”). Empty squares are noted using digits 1 through 8 (the number of empty squares), and “/” separates ranks.

The second field denotes whose move it is now. “w” depicts that it is White’s turn to play and “b” indicates that it is Black’s turn to play

CodeVita Problem Statement

Given a board position in FEN format, your task is to find out all the move(s) that Bishop(s) of the playing side can make.

Input Format:

  1. First line contains single FEN record, which corresponds to a particular board position and also indicates whose turn it is.

Output Format:

  1. The output must be printed as follows
  2. All legal moves that Bishop can make must be in the format “[<Move Format>]”
  3. Where <Move Format> is move represented in format “[fromSquare][toSquare]
  4. See Example section for better understanding of output format.
  5. Follow Output printing specification to print the output in required format.

Constraints:

  1. Since we focus on only first two parts of the FEN, we are essentially ignoring possibility of Castling. Hence our test cases don’t contain FENs which give rise to such positions.

Sample Input and Output


Sample 1 input: 3k4/8/8/2P1P3/3B4/2R1R3/8/4K3 w

Board Depiction 1:  



Output 1: []

Sample 2 input: 3k4/8/8/2P1P3/3B4/2R1R3/8/4K3 w

Board Depiction 2: 



Output 2:  [d4a7, d4b6, d4c5]

Sample 3 input: 3k4/8/8/2P1P3/3B4/2R1R3/8/4K3 w

Board Depiction 3: 



Output 3: [d4h8, d4a7, d4g7, d4b6, d4f6, d4c5, d4e5, d4c3, d4e3, d4b2, d4f2, d4a1, d4g1, b1h7, b1g6, b1f5, b1e4, b1d3, b1a2, b1c2]

Print Specification:

  1. Should start with “[” and end with “]”
  2. If more than one move is possible, moves should be separated by a comma followed by whitespace
  3. Moves of a single bishop should be printed in Move Format. Scan the board from 8th rank to 1st rank from a-file to h-file. Whichever square gets hit first, that move should be printed first.
  4. If more than one bishop exists for side to move, then start scanning for bishop from 8th rank to 1st rank, left to right i.e. from a-file to h-file. Whichever bishop appears first, print all moves for that bishop first.
  5. Verify your understanding of how printing should happen against examples shown above

TCS codevita 2016 Problem 5 : Continents and Oceans

There will be several continents on a world map. Idea is to identify the number of continents and the mass in each.

The land is denoted by ‘#’ and ocean by spaces. You have to find the number of continents (connected masses of land) and print the number of # in the continent.

Connected mass of land means that # can be connected in any way i.e. horizontally, vertically or diagonally.

Input Format:

  1. Path to a file, for example /tmp/T1.txt
  2. This file contains 15 rows and 60 columns depicting a map. See Examples section below.

Output Format:

Number of # per continent in descending order of size and total number of continents in the format depicted below

Island 1: <Biggest mass> say 100
Island 2: <Smallest mass> say 50
Number of continents: 2

Example 1:


Output:
Island 1: 593
Island 2: 98
Number of continents: 2

So Output is:
Island 1: 593
Island 2: 98
Number of continents: 2

Example 2:

Output:
Island 1: 805
Island 2: 68
Number of continents: 2

TCS codevita 2016 Problem 6 : 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: :

  1. A win results in 2 points, a draw results in 1 point and a loss is worth 0 points.
  2. The team with most goals in a match wins the match.
  3. Goal Difference (GD) is calculated as Goals For (GF) – Goals Against (GA).
  4. Teams can play maximum of two matches against each other – Home and Away matches respectively
  5. Ranking is decided as follows
    1. Team with maximum points is ranked 1 and minimum points is placed last
    2. Ties are broken as follows
    3. Teams with same points are ranked according to Goal Difference(GD).
    4. If Goal Difference(GD) is same, then team with higher Goals For is ranked ahead
    5. If GF are same, the teams should be at the same rank but they should be printed in case-insensitive alphabetic according of the team names.
  6. More than 2 matches of same teams, should be considered as Invalid Input
  7. 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:

  1. 0<N<=10,000
  2. 0<=S1,S2

Example:

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

Match 1: Spain vs. England (3-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

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:

TCS codevita 2016 Problem 7 : Mate In One

Background

A Chess board position is accurately captured by Forsyth-Edwards notation and is abbreviated as FEN. A FEN “record” defines a particular game position, all in one line of text and using only the ASCII character set. A FEN record consists of six fields. A complete description of the FEN format to represent Chess positions can be found here.

For the purpose of this problem, only consider first of the six fields of FEN. Before we describe the problem, let us look at how FEN maps to a board position. The following 5 images show board positions and its corresponding FEN representation.

Figure 1

This board position (above) depicts initial position before any side has made a move. In FEN format this board position is represented as

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

Let’s say, White plays e4. Then the board position looks like shown below.

figure 2

This board position (above) depicts the Chessboard after White has played e4. In FEN format this board position is represented as

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR

Similarly, 3 more half-moves are depicted in following diagrams
figure 3
figure 4
figure 5
The FENs corresponding to Figure 3, 4 and 5 are represented as 
           3. rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR
           4. rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR
           5. rnbqkbnr/pppp1ppp/8/8/4Pp2/8/PPPP2PP/RNBQKBNR 
Wikipedia describes first field of FEN format as follows 
Piece placement (from white’s perspective). Each rank is described, starting with rank 8 and ending with rank 1; within each rank, the contents of each square are described from file “a” through file “h”. Following the Standard Algebraic Notation (SAN), each piece is identified by a single letter taken from the standard English names (pawn = “P”, knight = “N”, bishop = “B”, rook = “R”, queen = “Q” and king = “K”).[1] White pieces are designated using upper-case letters (“PNBRQK”) while black pieces use lowercase (“pnbrqk”). Empty squares are noted using digits 1 through 8 (the number of empty squares), and “/” separates ranks 
Statement 
Given a board position in FEN format, your task is to find out all move(s) that lead to a forced mate. White to play and win in 1 move. 
Input Format: 
  1. First line contains single FEN record, which corresponds to a particular board position
  2. Second line contains -1 which indicates the end of input
Output Format: 
  1. The output must be printed as follows 
  • A string “Mating moves ” followed by “[<move format>]”
  • Where <move format> is move represented in format “[Piece][fromSquare]-[Piece][toSquare]”
  • If a mating move involves a capture then represent it as “[Piece][fromSquare]-[Piece]x[toSquare]”
  • If the Piece inflicting a mate is a pawn represent it only as [fromSquare]-[toSquare]
  • If there is more than one mating move, then follow the rules given below 
  1. Moves must be sorted alphabetically according to their ascii values
  2. Two moves must be separated by a comma followed by a white space characters
  3. Between the last move and terminating “]” character there should be no space or comma

2. See Example section for better understanding of output format

Constraints:
  1. The board position will always be White to move and mate in 1
  2. Since we focus on only first part of the FEN, we are essentially ignoring possibility of Castling being a mating move. Hence our test cases don’t contain FENs which give rise to such positions.
  3. There is no need to handle En Passant positions. There are no test cases involving En Passant moves.
  4. No need to implement pawn promotion rules. Our test cases do not contain positions which will lead to a pawn getting promoted and inflicting a mate.
  5. There are no test cases in which capture by a white pawn leads to a mate.
SNo.

Input

Output

1


3k4/8/3p4/7B/3Q4/2R1R3/8/5K2

-1


Mating moves [Qd4-Qxd6]

2


8/R7/3k4/8/2Q1P1B1/8/8/2K1R3
-1


Mating moves [Qc4-Qc7, Qc4-Qd5, e4-e5]

Explanation: 

Board position for sample input 1: 

Figure 6

Board position for sample input 2: 


Figure 7

TCS codevita 2016 Problems 8: Where’s my car!

The year is 2050. The population surge has taken over our metropolitan cities. High rise buildings are on a rise. And as usual, with the increase in population, the problem of parking in the city has increased manifold.

To reduce the problem of parking, the government has built multi-storey parking lots all over the city. Imagine the city as an X-Y grid. And there are roads connecting all the neighbouring grid points except diagonals. And there is a pre-defined intersection interval ‘I’ for parking lots such that at every I th intersection, there is a parking lot, starting from (0,0). For example, for a city of grid size 4×7 and I=3, you’ll have 6 parking lots at (0,0), (0,3), (0,6), (3,0), (3,3) and (3,6).

Now all the cars have been fitted with self-driving mechanism. So whenever you get out of a car at any point in the grid, it will choose the nearest parking lot and automatically drive to it. If two parking lots are at equal distance from where you left, it will choose the parking lot with the lowest X-coordinate first, and if X-coordinates are same, the lowest Y-coordinate.

At the parking lot, the cars will start getting parked from the ground floor and in the first available slot. As each floor gets filled up, newer cars will start parking on floors above them. Assume all the parking lots in the city have unlimited number of floors and a common maximum capacity of each floor ‘C’.

Now whenever the owner wants to know where his car is parked or wants to retrieve it, he’ll open the app ‘Where’s my car!’ and insert his car number and the app will tell him the coordinates of the parking lot, the floor number and the slot number.

Input Format:

The first line will contain a positive integer T determining the number of test cases.
For each test case, the first line will contain four positive integers X, Y, I and C, delimited by space where,

  1. X,Y denote the grid size of the city,
  2. I denotes the intersection interval for parking lots
  3. C denotes the maximum capacity of a floor in all the parking lots in the city.

The next line will contain a positive integer N denoting the number of events following.

  1. An event can be parking of a car(P) or retrieval of a car(R ).
  2. The next N lines will begin with either a character ‘P’ or ‘R’.
  3. If the line begins with P, it will also contain two positive integers, x and y denoting the current coordinate of the car where it is left off and has to drive itself to the nearest parking lot, and the car number S which is a unique 10-digit number for each car and can contain a mix of numbers and alphabet.
  4. If the line begins with ‘R’, it will contain the car number to retrieve.

Output Format:

For each test case, output the following.
First output the total number of parking lots in the city.
Then, for every retrieval event in the input list, output 4 space separated integers, p,q,r,s where

  1. p,q is the coordinate of the parking lot where the car is parked in,
  2. r is the floor number and s is the slot number.
  3. For brevity, output each number p,q,r,s, modulo 10.

Constraints:

  1. 11<=T<=10
  2. 1<= X,Y < 2,000,000,000
  3. 1<= C,N < 100,000
  4. 1<= I <= X,Y
  5. 0<= p <= X
  6. 0<= q <= Y
  7. Max no. of parking lots < 1,000,000

Sample Input and Output

TCS codevita 2016 Problem 9: Cats & Dogs 

Cats & Dogs have become friends now. They are going to a picnic and they decided to sit together in groups to have their lunch.

ReadBest answer of tcs codevita 2015 questions

There are N Cats & M Dogs. They can form any number of groups to sit together, subject to rules below.
Rules:

They will sit in a straight line
A Cat cannot sit beside another Cat or alone
A Dog cannot sit beside another Dog or alone
You need to find minimum number of groups they need to form. If no group can be formed then print -1.
Input Format:
First line of input will be a number of Test Cases T
Next T lines of input will contains two numbers N and M, delimited by space
Output Format:
Print minimum number of groups to be formed for each Test Case in new line
OR
Print -1 if no group can be formed
Constraints:
  1.  0<T<=10^6
  2.  0<N,M<=10^18

Example: Sample input and output

S.No.

Input

Output

1


2

5 5

1 3


1

-1

2


3

7 8

3 5

6 7


1

2

1

TCS codevita 2016 Problems 10: Christmas Tree

Chirag is a pure Desi boy. And his one and only dream is to meet Santa Claus. He decided to decorate a Christmas tree for Santa on coming Christmas. Chirag made an interesting Christmas tree that grows day by day.

The Christmas tree is comprised of the following Parts:

  1. Stand 
  2. Each Part is further comprised of Branches. 
  3. Branches are comprised of Leaves.

How the tree appears as a function of days should be understood. Basis that print the tree as it appears on the given day. Below are the rules that govern how the tree appears on a given day. Write a program to generate such a Christmas tree whose input is number of days.

Rules:

  1. If tree is one day old you cannot grow. Print a message “You cannot generate christmas tree” 
  2. Tree will die after 20 days; it should give a message “Tree is no more” 
  3. Tree will have one part less than the number of days. E.g. On 2nd day tree will have 1 part and one stand., On 3rd day tree will have 2 parts and one stand On 4th day tree will have 3 parts and one stand and so on. 
  4. Top-most part will be the widest and bottom-most part will be the narrowest. 
  5. Difference in number of branches between top-most and second from top will be 2 
  6. Difference in number of branches between second from top and bottom-most part will be 1.
Below is an illustration of how the tree looks like on 4th day

Input Format:

First line of input contains number of days denoted by N

Output Format:

Print Christmas Tree for given N

OR

Print “You cannot generate christmas tree” if N <= 1

OR

Print “Tree is no more” if N > 20

Constraints:

0<= N <=20

Sample Input and Output

Input:  2

Output:( below)


 Also read: TCS codevita old questions for practice

So that’s all in this big list TCS codevita 2016 Problems. If you know any new question post it here. I rectified all the errors from this “TCS codevita 2016 problems” list. But if you find any errors tell me in comment below. You can also post your answer in the comment. Thank you.

Leave a Comment

Exit mobile version