TCS Coding Question 2 Day 1 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 Coding Question 2 Day1 slot1

TCS Coding Question Day 1 Slot 1 – Question 2

Problem Statement – Given a string S(input consisting) of ‘*’ and ‘#’. The length of the string is variable. The task is to find the minimum number of ‘*’ or ‘#’ to make it a valid string. The string is considered valid if the number of ‘*’ and ‘#’ are equal. The ‘*’ and ‘#’ can be at any position in the string.

Note : The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in the input string.

  • (*>#): positive integer
  • (#>*): negative integer
  • (#=*): 0

Example 1:

Input 1:

  • ###***   -> Value of S

Output :

  • 0 → number of * and # are equal

15 comments on “TCS Coding Question 2 Day 1 Slot 1”


  • Sakshi

    #include
    using namespace std;

    int main()
    {
    string s;
    int a = 0, b = 0;
    cin >> s;
    for (int i = 0; i b)
    cout << "negative integer";
    else
    cout << "positive integer";

    return 0;
    }


  • swejalreddy

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int count1=0,count2=0;
    System.out.println(“enter the string”);
    String s=sc.nextLine();
    char[] ch = s.toCharArray();
    int l=s.length();
    for(int i =0;icount2)
    {
    System.out.println(count1-count2);
    }
    else
    {
    System.out.println(count1-count2);
    }

    }
    }


  • baishakhibehera14

    s=input(“enter the string”)
    a=0
    b=0
    for i in s:
    if i == ‘*’:
    a=a+1
    elif i == ‘#’:
    b=b+1
    if a > b:
    x = a – b elif b > a:
    x = a- b
    elif a == b:
    x = a- b
    print(x)


  • shreeyeshmhatre50

    public class Question{
    public static void main(String[] args) {
    System.out.println(“Enter String:”);
    Scanner sc=new Scanner(System.in);
    String input=sc.nextLine();
    int count1=0,count2=0;

    for(int i=0;icount2)
    {
    System.out.println(“Positive integer”);
    }
    else
    {
    System.out.println(“negative integer”);
    }
    }


  • Shreyanshi

    #include
    #include
    int main(){
    int i ,j,k=0,count=0;
    char string[]=”hello*** i am## shreyanshi”;
    for ( i=0;string[i]!=’\0′;i++){
    if(string[i]==’*’){
    j+=1;
    }
    else if (string[i]==’#’){
    k+=1;
    }

    }

    printf(“%d\n%d\n%d\n”,i,j,k);
    if(j==k){
    printf(“%d” ,count);
    }
    else if(k<j){
    printf("positive integer");
    }
    else if(j<k){
    printf("negative integer");
    }
    return 0;
    }


  • Rohit

    C++ Code

    #include
    using namespace std;
    int main()
    {
    int a=0,b=0;
    string s;
    cin>>s;
    for(int i=0; i<s.length(); i++){
    if(s[i]=='#')
    a++;
    else if(s[i]=='*')
    b++;

    }
    cout<<b-a;
    }


  • Pragya

    #include

    using namespace std;

    int main() {
    // Write C++ code here
    string s;
    cin>>s;
    int x=count(s.begin(),s.end(),’*’);
    int y=count(s.begin(),s.end(),’#’);
    if(x==y)
    {
    cout<<0;
    }
    else if(x<y)
    {
    cout<<"negative integer";
    }
    else
    {
    cout<<"Positive integer";
    }

    return 0;
    }


  • Akankshya

    #PYTHON
    st = input()
    c1,c2=0,0
    for i in range(0,len(st)):
    if(st[i]==”*”):
    c1=c1+1
    else:
    c2=c2+1
    if c1==c2:
    print(0)
    elif c1>c2:
    print(“positive integer”)
    else:
    print(“negative integer”)


  • Gyanendra

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner scn=new Scanner(System.in);

    String str=scn.nextLine();

    int count1=0,count2=0;
    for(int i=0;i<str.length();i++){

    if(str.charAt(i)=='*'){
    count1++;
    }
    if(str.charAt(i)=='#'){
    count2++;
    }

    }
    int ans= count1-count2;
    System.out.print("Total number: "+ans);
    }
    }


  • Abhishek

    ……………………….THE FOLLOWING ANSWER WILL ONLY CLEARR YOUR TEST CASES IN NQT EXAM……………………………..BY ABHISHEK PATIL
    n=str(input())
    m=str(input())
    if len(n)==len(m):
    print(“0”)
    if len(n)>len(m):
    print(len(n)-len(m))
    if len(n)len(m):
    print(len(n)-len(m))
    if len(n)>len(m):
    print(len(n)-len(m))


    • Abhishek

      n=str(input())
      m=str(input())
      if len(n)==len(m):
      print(“0”)
      if len(n)>len(m):
      print(len(n)-len(m))
      if len(n)len(m):
      print(len(n)-len(m))
      if len(n)>len(m):
      print(len(n)-len(m))


      • Abhishek

        n=str(input())

        m=str(input())

        if len(n)==len(m):

        print(“0”)

        if len(n)>len(m):

        print(len(n)-len(m))

        if len(n)len(m):

        print(len(n)-len(m))

        if len(n)>len(m):

        print(len(n)-len(m))


  • Rahul

    rah = “####*****”
    raj = rah.count(“#”)
    ram = rah.count(“*”)
    if(raj>ram):
    print(“Positive integer”)
    elif(raj==ram):
    print(“0”)
    else:
    print(“Negative integer”)


    • Rahul

      rah = “###***”
      raj = rah.count(“#”)
      ram = rah.count(“*”)
      if(raj>ram):
      print(“Positive integer”)
      elif(raj==ram):
      print(“0”)
      else:
      print(“Negative integer”)