TCS Coding Questions 2022 Day 2 Slot 1

Coding Question 2 for 2022 (September slot)

In this article, we will discuss about the TCS Coding Question which is asked in the TCS placement test. This type of Coding Questions will help you to crack your upcoming TCS exam as well as during your inteview process.

tcs nqt coding question

 

TCS Coding Question Day 2 Slot 1 – Question 2

At a fun fair, a street vendor is selling different colours of balloons. He sells N number of different colours of balloons (B[]). The task is to find the colour (odd) of the balloon which is present odd number of times in the bunch of balloons.

Note: If there is more than one colour which is odd in number, then the first colour in the array which is present odd number of times is displayed. The colours of the balloons can all be either upper case or lower case in the array. If all the inputs are even in number, display the message “All are even”.

Example 1:

  • 7  -> Value of N
  • [r,g,b,b,g,y,y]  -> B[] Elements B[0] to B[N-1], where each input element is sepārated by ṉew line.

Output :

  • r -> [r,g,b,b,g,y,y]  -> “r” colour balloon is present odd number of times in the bunch.

Explanation:

From the input array above:

  • r: 1 balloon 
  • g: 2 balloons
  • b:  2 balloons
  • y : 2 balloons 

Hence , r is only the balloon which is odd in number.

Example 2:

Input:

  • 10 -> Value of N
  • [a,b,b,b,c,c,c,a,f,c] -> B[], elements B[0] to B[N-1] where input each element is separated by new line.

Output :

b-> ‘b’ colour balloon is present odd number of times in the bunch.

Explanation:

From the input array above:

  • a: 2 balloons
  • b: 3 balloons 
  • c: 4 balloons 
  • f: 1 balloons 

Here, both ‘b’ and ‘f’ have odd number of balloons. But ‘b’ colour balloon occurs first.

Hence , b is the output.

Input Format for testing

The candidate has to write the code to accept: 2 input 

  • First input: Accept value for number of N(Positive integer number).
  • Second Input : Accept N number of character values (B[]), where each value is separated by a new line.

Output format for testing

The output should be a single literal (Check the output in example 1 and example 2)

Constraints:

  • 3<=N<=50
  • B[i]={{a-z} or {A-Z}}

21 comments on “TCS Coding Questions 2022 Day 2 Slot 1”


  • thangavelmariyappan298

    c solution
    #include

    int main() {
    int n;
    scanf(“%d”,&n);
    char ar[n+1];
    scanf(“%s”,ar);
    int freq[n];
    for(int i=0;i<n;i++){
    freq[i] = -1;
    }

    for(int i=0;i<n;i++){
    if(freq[i]!=0){
    int count = 1;
    for(int j=i+1;j<n;j++){
    if(ar[i]==ar[j]){
    count++;
    freq[j] = 0;
    }
    }
    if(count%2!=0){
    printf("%c",ar[i]);
    return 0;
    }
    }
    }
    printf("All Are Even Numbers");
    return 0;
    }


  • simranmandal4459

    C++ Solution

    #include
    #include
    using namespace std;

    int main()
    {
    int n;
    cin >> n;
    char arr[n];

    map mp;

    for(int i=0; i> arr[i];

    mp[arr[i]]++;
    }

    for(auto i:mp){
    if(i.second & 1){
    cout << i.first;
    break;
    }
    }

    return 0;
    }


  • ramkrishnak19602

    baloon=list(input().split(” “))
    count=0
    min=1000
    new_array=[0]*1000
    for x in baloon:
    new_array[ord(x)]=new_array[ord(x)]+1
    for i in range(0,len(new_array)):
    if new_array[i]%2!=0 :
    count=1
    if baloon.index(chr(i))<min:
    min=baloon.index(chr(i))
    if count==1:

    print(baloon[min])

    Python code
    I guess complexity is O(n)


  • Raghul

    from collections import Counter

    c = Counter([“a”,”b”,”b”,”b”,”c”,”c”,”c”,”a”,”f”,”c”])

    for i in c.keys():
    if c[i] & 1 == 1:
    print(“Odd : “,i)
    break


  • Raghul

    Here is My Thoughts…,

    from collections import Counter

    c = Counter([“a”,”b”,”b”,”b”,”c”,”c”,”c”,”a”,”f”,”c”])

    for i in c.keys():
    if c[i] & 1 == 1:
    print(“Odd : “,i)
    break