TeachingBee

Previously Asked TCS Digital Coding Questions

TCS Digital Coding Questions

TCS Digital Recruitment Drive has an Advance Coding round. TCS Digital Coding Questions round is different from the regular Coding round which is required in TCS. There will be two problems statements, and each will have a the total of two accessible and 3 hidden test cases. The topics which are generally tested in TCS Coding Questions are Arrays, Strings, Data Structures, Algorithms Based and Math-Based concepts.

In this post we will discuss some of the TCS Digital Coding Questions which are previously asked.

TCS Coding Questions Format

Number of Questions2
Time Allotted60 mins
Cut OffOutput is Necessary
TCS Digital Coding Questions Format

TCS Digital Coding Questions

1. Sum of the digits of the given positive integer number N is UNO or not.

Problem Statement

Given a positive integer number N, reduce the number of digits of N by computing the sum of all the digits to get a new number. If this new number exceeds 9, then sum the digits of this new number to get another number and continue this way until a single digit value is obtained as the ‘digit sum’. The task here is to find out whether the result of the digit sum done this way is ‘1’ or not. If the result is 1 return UNO else not.

Algorithm

Idea is to keep doing sum until sum is not less than or equal to 9

Implementation

// TCS Digital Coding Questions

int sumOfDigits(int n) {
  int sum = 0;

  while (n > 0 || sum > 9) {
    if (n == 0) {
      n = sum;
      sum = 0;
    }
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

int main() {
  int n = 235;
  if (sumOfDigits(n) == 1) {
    cout << "UNO";
  } else {
    cout << "Not UNO";
  }

  return 0;
}

Another Approach

  1. A number can be represented in either of the form 9n or 9n + k. 
  2. If number is of 9n then its sum of digits will also be of the form 9n. Therefore, final single digit sum will always be 9 at last. For eg 378 ==> 3+7+8 ==>18 ==> 1+8 ==> 9
  3. If number is of 9n + k then final single digit sum will always be k. For eg 367 (9*40 +7 ) ==> 16 ==> 7.
  4. So if n%9==0 sum of digits will be 9 else sum of digits will be n%9.

Implementation

// TCS Digital Coding Questions

int sumOfDigits(int n) {
  // Base case
  if (n == 0)
    return 0;

  return (n % 9 == 0) ? 9 : (n % 9);
}

int main() {
  int n = 235;
  if (sumOfDigits(n) == 1) {
    cout << "UNO";
  } else {
    cout << "Not UNO";
  }
  return 0;
}

2. Find out whether the jet plane returns to the same position from where it took off.

Problem Statement

Most modern aircrafts are equipped with an autopilot system – one of the most useful features in fighter jets. In the beta testing of autopilot mode, one of the inputs is a string of literals containing the directions, which is fed into the flight computer before the take-off. The jet plane is put on an auto-landing mode that enables the plane to land automatically once all the directions in the string are complete. Given the directions in the string “str”, the task here is to find out whether the jet plane returns to the same position from where it took off.

  • Each direction in the string changes at an interval of 1 minute(1< =i<= N), where N is the number of directions in the input string.
  • The directions are North (N), South(S), West(W) and East(E).

Output:

  • “Returned successfully”, if the plane returns to the starting position.
  • “Not returned successfully”, if the plane does not return to the starting position.

Example 1: Input: NESW Output: Returned successfully

Example 2: Input:  NNWESW. Output: Not returned successfully

Algorithm:

1. The idea is to keep track of x and y coordinate of jet plane.
2. For 'N' 'increment y coordinate, for 'E' increase x coordinate . Similarly for 'S' and 'W' decrease y and x coordinate respectively.
3. Lastly check if both x and y are 0 if yes jet plane has returned successfully else no.

// TCS Digital Coding Questions

public class Main {
    static int getJetDirections(String directions) {
        int x = 0, y = 0;
        for (int i = 0; i < directions.length(); i++) {

            if (directions.charAt(i) == 'N') {
                y++;
            }
            if (directions.charAt(i) == 'E') {
                x++;
            }
            if (directions.charAt(i) == 'W') {
                x--;
            }
            if (directions.charAt(i) == 'S') {
                y--;
            }
        }
        if (x == 0 && y == 0)
            return 1;
        else
            return 0;
    }
    public static void main(String[] args) {
        String str = "NESW";
        if (getJetDirections(str) == 1)
            System.out.println("Returned successfully");
        else
            System.out.println("Not Returned successfully");

    }
}

3. Possible combinations of the coins that can be inserted to get rupees from the kiosk.

A ‘coin vend’ kiosk is installed all the major metro stations The machine allows one to obtain cash of ‘R’ rupees in exchange for coins. The machine operates with the following conditions:

  1. Only coins of denomination 1 rupee and 2 rupee can be exchanged.
  2. Coins of denomination 2 rupees should not be inserted successively twice.

The task here to find all the possible combinations of the coins that can be inserted to get rupees from the kiosk.

Input: 3 (1 + 1 + 1), (2 + 1), (1 + 2), Output: 6

Algorithm:

The idea is to use recursion. If 1 is inserted then we can insert 1 and 2 both. But if we have inserted 2 then again 2 cannot be inserted so only 1 can be inserted. Thus, are recursion becomes coinCombinations(n - 1) + coinCombinations(n - 3); [ As 2 comes in combination with 1 so 3 will be subtracted instead of 1 ]

Implementation

// TCS Digital Coding Questions

class Main {
    static int coinCombinations(int n) {
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        return coinCombinations(n - 1) + coinCombinations(n - 3);
    }

    public static void main(String[] args) {
        int n=5;
        System.out.println(coinCombinations(n));
    }
}

4. Find the number of students whose height is less than the height of their adjacent students.

Problem Statement

A physical education teacher asks students to assemble in a straight line for the morning assembly.Given an array of N in which each element represents the height of the student in that position. The task here is to find the number of students whose height is less than the height of their adjacent students.

Input: 35, 15, 45,25,55 Output: 2 (35>15<45 and 45>25<55)

Algorithm

The idea is to traverse array and compare with its left and right adjacent elements.If it is less than both the elements increment the count

Implementation

// TCS Digital Coding Questions

public class Main {
    public static void main(String[] args) {
        Integer[] arr = {
            35,
            15,
            45,
            25,
            55
        };
        int count = 0;
        for (int i = 1; i < arr.length - 1; i++) {
            if (arr[i - 1] > arr[i] && arr[i + 1] > arr[i]) {
                count++;
            }
        }
        System.out.println(count);
    }
}

5. Find the largest number among them which does not contain 9. 

Problem Statement

Rakesh once had trouble finding the numbers in a string. The numbers are distributed across a string.You need to parse only those numbers which do not contain 9. For eg, if the string contains “hello this is alpha 5051 and 9475 TCS Coding Questions”. You will extract 5051 and not 9475. Print the largest number.

Input: This is alpha 5057 and 97 Output: 5057

Algorithm:

1. The idea is to traverse string and check if it is a digit.
2. If digit has been encountered loop until character is
found and form the number from these digits.
3. If the number doesn't contains 9 then find the maximum from current max and the number formed.

Implementation

// TCS Coding Questions

public class Main {
    static long ExtractNumber(String S) {
        long ans = -1;
        long result = 0;
        for (int x = 0; x < S.length(); ++x) {
            // check if it is a digit.
            if (Character.isDigit(S.charAt(x))) {
                boolean temp = true;
                result = 0;

                // Form number from digits encountered.
                while (x < S.length() && Character.isDigit(S.charAt(x))) {
                    int value = (int) Integer.parseInt(S.valueOf(S.charAt(x)));

                    // check if number contains 9. If yes, we don't need to consider that number. 
                    if (value == 9) {
                        temp = false;
                    }

                    result = result * 10 + value;

                    ++x;
                }

                // Consider only non 9 digit numbers
                if (temp) {
                    ans = Math.max(result, ans);
                }
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        String s = "hello this is alpha 5051 and 9475";
        System.out.println(ExtractNumber(s));

    }

}

6. Order Management

Problem Statement:

A store has different categories of products in stock as shown below.
Item Number=[101, 102, 103, 108] Price=[42, 50, 500, 40] Stock =[10, 20, 15, 16]

User Inputs two values:

  • Item number for item which user wish to buy
  • Quantity for the item entered above
  1. If quantity is less than stock and item is available display a notification message showing Output: Total price in float with precision and updated stock for item after after purchase
  2. If the quantity and stocks less than quantity entered by the user while placing order, then Output: NO STOCK and quantity left
  3. If user enter character as input for item number and quantity or enter item number which is not available Output: INVALID INPUT

Algorithm

The idea is to make hashmap with key as item number and value as price and stock. Check if item number is present or not. If present return required result or else return Invalid input.

Implementation

// TCS Coding Questions

import java.util.*;
class Item {
    int price;
    int stock;
    Item(int p, int s) {
        this.price = p;
        this.stock = s;
    }
}

public class Main {

    public static void main(String[] args) {
        int[] itemNumber = {
            101,
            102,
            103,
            108
        };
        int[] price = {
            42,
            50,
            500,
            40
        };
        int[] stock = {
            10,
            20,
            15,
            16
        };
        HashMap < Integer, Item > itemStore = new HashMap < > ();

        for (int i = 0; i < itemNumber.length; i++) {
            Item item = new Item(price[i], stock[i]);
            itemStore.put(itemNumber[i], item);
        }

        int user_input = 101;
        int user_quantity = 2;

        if (itemStore.containsKey(user_input)) {
            Item item = itemStore.get(user_input);
            if (item.stock < user_quantity) {
                System.out.println("No Stock");
                System.out.println(item.stock + " Quantity left");
            } else {
                System.out.println("Total Price: " + item.price * user_quantity);
                System.out.println(item.stock - user_quantity + " Quantity left");
                Item itemUpdated = new Item(item.price, item.stock - user_quantity);
                itemStore.put(user_input, itemUpdated);
            }
        } else {
            System.out.println("Invalid Input");
        }

    }

}

7. Split String into three palindromic substrings

Problem Statement:

Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes.

Input: str = “ababbcb”
Output: a bab bcb 
Explanation: Possible splits are {“aba”, “b”, “bcb”} and {“a”, “bab”, “bcb”}.  Since, {“a”, “bab”, “bcb”} has splits at earlier indices, it is the required answer.

Algorithm:

The idea is to run two loops and checking palindrome property for every 3 substrings.

Implementation

// TCS Digital Coding Questions

public class Main {

    // Check If String is Pallindrome
    static boolean checkPallindrome(String str) {

        int i = 0, j = str.length() - 1;
        while (i < j) {
            if (str.charAt(i) != str.charAt(j))
                return false;
            i++;
            j--;
        }

        return true;
    }

    public static void main(String[] args) {
        String s1, s2, s3;
        String s = "ababbcb";
        int n = s.length();
        for (int i = 1; i < n - 1; i++) {
            // Substring one
            s1 = s.substring(0, i);
            // If substring is pallindrome
            if (checkPallindrome(s1)) {
                for (int j = 1; j < n - i; j++) {
                    // Check if Substring two and Substring three is pallindrome
                    s2 = s.substring(i, i + j);
                    s3 = s.substring(i + j, n);
                    if (checkPallindrome(s2) && checkPallindrome(s3)) {
                        System.out.println(s1);
                        System.out.println(s2);
                        System.out.println(s3);
                        return;
                    }
                }
            }
        }
        System.out.println("Not Possible");
        return;
    }

}

Above solution is not optimal.For Optimal Solution check here.

Conclusion

In this post we discussed tcs coding questions that are previously asked. Hope these tcs digital coding questions will help you to prepare for tcs coding questions rounds.

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

uses of computer network

Important Uses Of Computer Network

In this article we will see an overview of computer networks, various uses of computer network and its applications, and key technologies involved in computer networking. So, let’s begin. What

AWS Lambda Interview Questions-teachingbee

Important AWS Lambda Interview Questions And Answers

In today’s fast-paced tech landscape, AWS Lambda has emerged as a game-changer in serverless computing. As organizations increasingly shift towards serverless architectures, mastering AWS Lambda is becoming a crucial skill

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In