Top 10 TCS Interview Questions and Answers

KK EDU SOLUTIONS
0

 

Top 10 TCS Interview Questions and Answers


Q1. Who Won the Election???

  • Elections are going on, and there are two candidates A and B, contesting with each other. There is a queue of voters and in this queue, some of them are supporters of A and some of them are supporters of B. Many of them are neutral. The fate of the election will be decided on which side the neutral voters vote. Supporters of A and supporters of B make attempts to win the votes of neutral voters.

    The way this can be done is explained below:

    1. The voter queue is denoted by three characters, viz {-, A, B}. The ‘-’ denotes neutral candidate, ‘A’ denotes supporter of candidate A and ‘B’ denotes supporter of candidate B. 2. Supporters of A can only move towards the left side of the queue. 3. Supporters of B can only move towards the right side of the queue. 4. Since time is critical, supporters of both A and B will move simultaneously. 5. They both will try and influence the neutral voters by moving in their direction in the queue. If a supporter of A reaches the neutral voter before a supporter of B reaches him, then that neutral voter will become a supporter of candidate A. 6. Similarly, if a supporter of B reaches the neutral voter before supporter of A reaches him, then that neutral voter will become a supporter of candidate B. 7. Finally, if both reach at the same time, the voter will remain neutral. A neutral vote cannot decide the outcome of the election. 8. If finally, the queue has more votes for candidate A, then A wins the election. If B has more votes, then B wins that election. If both have equal votes, then it will be a coalition government.

    Your task is to find the outcome of the election.

    For Example:
    Given string- “B--A-” B ---> B A <--- A B -----------------------------> Output - B as B can move towards right only and A can move in left direction only. Thus B has 3 supporters in total while A have only 2 supporters.

    Note:

    1. There are no test cases where all votes are neutral. 2. The influenced voters do not move and hence does not have any influence over the neutral voters.
    Input Format:
    The first line of the input contains a single integer T, representing the number of test cases. The first line of each test case will contain the string having characters ‘A’, ‘B’ or ‘-’
    Output Format:
    For each test case, you need to print ‘A’ if A wins the election, ‘B’ if B wins the election or ‘Coalition’ if both have equal votes. Output for each test case will be printed in a separate line.
    Note:
    You do not need to print anything. It has already been taken care of. Just implement the given function.
    Constraints:
    1 <= T <= 100 1 <= N <= 10^4, Where T is the number of testcases, and N is the length of the string. Time Limit: 1sec








Q2. Constellation

Given a matrix ‘UNIVERSE’ with 3 rows and ‘N’ columns, with the characters { # , * , . } and these characters represent a cluster of stars and galaxies in space. Stars are represented by ‘*’ symbol. The ‘.’ symbol represents empty space.

We define a constellation as a 3x3 matrix which contains stars in the shape of vowels. A group of constellations is defined as a galaxy. Note that a galaxy may contain many stars but they will never be overlapping. Two galaxies are separated by a column of ‘#’.

Given the ‘UNIVERSE’ matrix, print a string which denotes all the vowels formed by stars and ‘#’ present in the matrix.

A looks like this :
. * . * * * * . *
E looks like this :
* * * * * * * * *
I looks like this :
* * * . * . * * *
O looks like this :
* * * * . * * * *
U looks like this :
* . * * . * * * *
Note :
It is guaranteed that no two constellations are overlapping.
Input format :
The first line of input contains an integer ‘T’ denoting the number of test cases. The first line of each test case contains a single integer ‘N’ denoting the number of columns in the matrix. The next 3 lines of each test case denote the matrix of size 3 rows and ‘N’ columns which denotes the stars and the galaxies. Each line contains ‘N’ space separated characters from the set { ‘#’ , ’*’ , ’.’ }.
Output format :
For each test case, print a string which contains the shape of vowels and the hashes.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50 3 <= N <= 10^5 Time Limit : 1 sec




Post a Comment

0Comments
Post a Comment (0)