# TCS DCA Coding Questions and Answers This blog contains TCS Digital Coding questions that were previously asked in the TCS Elevate Wings DCA. It will be helpful in clearing the Digital Capability Assessment and make your promotion to Digital Cadre.

Good News For TCS Associates as Tata Consultancy Service is conducting DCA (Direct Capability Assessment) again in the month of September 2021. This is the third time TCS conducting DCA in the year 2021. Earlier it is organized in the month of January and April.

Eligibility

Full-time active employees in C1 or below grades with a total relevant experience of up to 3 years as of 31st Aug 2021 with Technical Qualifications

• BE/B.Tech, ME/M.Tech
• B.Sc/BCA and equivalent qualifications
• M.Sc/MCA/MS and equivalent qualifications

Assessment Date: 27th to 30th Sep

## Previously asked TCS DCA coding questions with solutions :

1 . Given a string str which consists of only 3 letters representing the color,(H) Hue, (S) Saturation, (L) Lightness, called HSL colors. The task is to count the occurrence of ordered triplet “H, S, L” in a given string and give this count as the output.

This question was asked in April 21 Digital Capababilty Assessment

Examples:

A) Input

HHSL

Output

2

Explanation: There are two triplets of RGB in the given string:

• H at index 0, S at index 2 and L at index 3 forms one triplet of HSL.
• H at index 1, S at index 2 and L at index 3 forms the second triplet of HSL.

B) Input: S = “SHL”
Output:
Explanation: No triplets exists.

Solution Approach

1. Count the number of L(Light) in the given string and store the value in a Light_Count variable.
2. Initialize Hue_Count = 0.
3. Iterate over all character of string from left to right.
4. If the current character is (H)Hue, increase the Hue_Count.
5. If current character is (L)Light, decrease Light_Count.
6. When the current character is S(Saturation) add Hue_Count * Light_Count in the result.

Solution in Java

``````// Java code for the above program
class GFG{

// function to count the
// ordered triplets (H, S, L)
static int countTriplets(String color)
{
int result = 0, Light_Count = 0;
int Hue_Count = 0;
int len = color.length();
int i;

// count the L(Lightness)
for (i = 0; i < len ; i++)
{
if (color.charAt(i) == 'L')
Light_Count++;
}

for (i = 0; i < len ; i++)
{
if (color.charAt(i) == 'L')
Light_Count--;
if (color.charAt(i) == 'H')
Hue_Count++;
if (color.charAt(i) == 'S')
result += Hue_Count * Light_Count;
}
return result;
}

// Driver Code
public static void main (String[] args)
{
String color = "HHSSLLHSSLL";
System.out.println(countTriplets(color));
}

}
``````

2. Check length of a string is equal to the number appended at its last

This question was asked in April 21 Digital Capababilty Assessment

Given a string that (may) be appended with a number at last. You need to find whether the length of string excluding that number is equal to that number. For example for “TechnoName10”, the answer is True as TechnoName consist of 10 letters. The length of String is less than 10, 000.

Examples :

```Input:  str = "Techn5"
Output:  Yes
Explanation : As Techn is of 5 length and at
last number is also 5.

Input:  str = "TechnoName15"
Output:  No
Explanation: As Technoname is of 10 length and
at last number is 15 i.e. not equal```

Solution Approach

1. Traverse string from end and keep storing the number till it is smaller than the length of the overall string.
2. If the number is equal to length of string except that number’s digits then return true.
3. Else return false.

Solution Code in Java

``````// Java program to check if size of
// string is appended at the end or not.
import java.io.*;

class GFG {

// Function to find if given number is
// equal to length or not
static boolean isequal(String str)
{
int n = str.length();

// Traverse string from end and find the number
// stored at the end.
// x is used to store power of 10.
int num = 0, x = 1, i = n - 1;
for (i = n - 1; i >= 0; i--)
{
if ('0' <= str.charAt(i) &&
str.charAt(i) <= '9')
{
num = (str.charAt(i) - '0') * x + num;
x = x * 10;
if(num>=n)
return false;
}
else
break;
}

// Check if number is equal to string
// length except that number's digits
return num == i + 1;
}

// Drivers code
static public void main(String[] args)
{
String str = "Technoname12";
if (isequal(str))
System.out.println("Yes");
else
System.out.println("No");
}
}

``````

3 . Write a program to remove 7 and 56 from given string.

This question was asked in Jan 21 Digital Capababilty Assessment

Examples :

1) Given String : Tec7hno56Name

Output : TechnoName

2) String =Tec7h5no6Name

Output : Tech5no6Name

Solution

``````#include <stdio.h>
#include <string.h>
int main()
{

char c ="Tec7hno56Name";
int n=strlen(c);
int i;
for(i=0;i<n;i++)
{
if(c[i]=='7')
{
c[i] = '\0' ;
}
if(c[i]=='5' && c[i+1]=='6')
{
c[i] = '\0' ;
c[i+1]='\0';
}
}

for(i=0;i<n;i++)
{
printf("%c",c[i]);
}
return 0;
}
``````

4 . Write a program to find multiplication of a given number

This question was asked in Jan 21 Digital Capababilty Assessment

Input : 1234

Output Logic : 1*2*3*4= 24

Solution

``````#include <stdio.h>
#include <string.h>
int main()
{
int n=1234;
int r,sum=1;
while(n>0)
{
r=n%10;
sum=sum*r;
n=n/10;
}
printf("Sum is %d ",sum);
}
``````

5 . You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return `0`

Example 1:

```Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
```

Example 2:

```Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.```

Solution

``````public class Solution {
public int maxProfit(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice)
minprice = prices[i];
else if (prices[i] - minprice > maxprofit)
maxprofit = prices[i] - minprice;
}
return maxprofit;
}
}``````

5 . Given a string, consisting of alphabets and digits, find the frequency of each digit in the given string.

##### Input Format

The first line contains a string, which is the given number.

##### Constraints

All the elements are made of English alphabets and digits.

##### Output Format

Print ten space-separated integers in a single line denoting the frequency of each digit .

a11472o5t6

###### Sample Output 0

0 2 1 0 1 1 1 1 0 0

Solution

``````#include<stdio.h>
#include<string.h>

int main ()
{
char a, freq = {0};
int i, n, j, count = 0;
scanf("%[^\n]s", a);    //input from user
n = strlen(a);         // finding string length
char ch = '0';
for (i = 0; i < 10; i++)
{
for (j = 0; j < n; j++)
{
if (a[j] == ch)
{
count++;
}
}
printf("%d ", count);
count = 0;
ch++;
}

return 0;
}

``````

6 . You will be given an array of integers and a target value. Determine the number of pairs of array elements that have a difference equal to a target value.

For example, given an array of [1, 2, 3, 4] and a target value of 1, we have three values meeting the condition: 2-1 = 1, 3-2 = 1, and 4-3 = 1.

Function Description

Write a function pairs. It must return an integer representing the number of element pairs having the required difference.

pairs has the following parameter(s):

• k: an integer, the target difference
• arr: an array of integers

Input Format

• The first line contains two space-separated integers n and k, the size of arr and the target value.
• The second line contains n space-separated integers of the array arr.

Sample Input

5 2

1 5 3 4 2

Sample Output

3

Solution

``````import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int [n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int counter = 0;
Set<Integer> value = new HashSet<Integer>();
for(int i : arr){
}
for(int i : value){
if(value.contains(i + k)){
++counter;
}
}
System.out.println(counter);
}
}``````

7 .Write a program to find the count of numbers that consists of unique digits.

Input:

Input consists of two Integer lower and upper value of a range

Output:

The output consists of a single line, print the count of unique digits in a given range. Else Print”No Unique Number

Solution:

Input –

10

15

``````#include<bits/stdc++.h>
using namespace std;

void printUnique(int l, int r)
{
int count=0;
for (int i=l ; i<=r ; i++)
{
int num = i;
bool visited = {false};

while (num)
{

if (visited[num % 10])
break;

visited[num%10] = true;

num = num/10;
}

if (num == 0)
count++;
}
if(count>0)
cout<<count;
else
cout<<"No Unique Number";
}

int main()
{
int l,r;
cin>>l>>r;
printUnique(l, r);
return 0;
} ``````

If you know some other important questions that were previously asked then you can leave a comment here or mail us at technonamecontact@gmail.com, So anyone can a get chance to publish their code on our website. As a result, one can improve his knowledge and spread with others

Please check most asked programming questions of strings with examples by clicking here, after that you might get a deep knowledge about string operations, in short, it will be helpful in clearing the interview

For understanding the most important concept data structures in an easy way you can check this article on our website.

### 16 responses to “TCS DCA Coding Questions and Answers”

1. Gopal says:

I really loved the approach of the solutions. Keep rocking!

• Akshat Jain says:

Thank you so much !! Stay connected with us

2. Atharva Bowlekar says:

Good.

• Akshat Jain says:

Thank you so much !! Stay connected with us

3. Avi says:

//Simple solution for 1st problem
public class Main
{
public static void main(String[] args) {

String str = “Techntra8”;
if (isequal(str))
System.out.println(“Yes”);
else
System.out.println(“No”);
}

static boolean isequal(String str)
{
int length = str.length()-1;

int lastNo = Character.getNumericValue(str.charAt(str.length()-1));
if (lastNo == length){
return true;
}else{
return false;
}
}
}

• Akshat Jain says:

Good efforts !! But it does not works when string length is greater than equal to 10 , Ex : if input string is “TechnoName10” Now based on your function it will search for last character which is 0 , so it fails here.
Try to optimize this code and let me know , we will publish your code.

4. Avi says:

//2nd problem

5. Unknown says:

In the 6th question, in sample test case shouldn’t the output be 3? As the target is 2 and there will 3 pairs with that difference : 3-1, 5-3 and 4-2.

• Akshat Jain says:

Yes , its updated now, thanks !!

6. shreyans jain says:

can we use python in dca

• Akshat Jain says:

Yes, we can use it
Varieties of other languages are also available.
Most users prefer C, Java and Python only.

7. Yogita Sahu says:

“`s=input()
st=0
n=0
for i in s:
if i.isnumeric():
n=10*n+int(i)
elif i.isalpha():
st+=1
# count=”.join(list(map(int,list(n))))
if st==n:
print(“Yes”)
else:
print(“No”)
““

8. Yogita Sahu says:

s=input()
s=list(s)
for i in range(len(s)-1):
if s[i]==’7′:
s[i]=”
elif s[i]==”5″ and s[i+1]==”6″:
s[i:i+2]=”
print(”.join(s))

9. Parth says:

Simpler python solution for 1st question

def fun(s:str)->int:
cnt = 0
hcnt = 0
lcnt = 0

for c in s:
if c==”H”:
hcnt+=1
elif c==”L”:
lcnt +=1
else:
cnt+=max(hcnt, lcnt)

return cnt

• Akshat Jain says:

Nice work

10. kiran says:

5 . Given a string, consisting of alphabets and digits, find the frequency of each digit in the given string.

pls provide python solution