0% found this document useful (0 votes)
31 views32 pages

TCS Ninja Coding Problems Solutions

Uploaded by

panjagalatheja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views32 pages

TCS Ninja Coding Problems Solutions

Uploaded by

panjagalatheja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SkillFactory

Problem Statement (TCS Ninja – Aug 2019 Slot 4)


Our hoary culture had several great persons since time immemorial and king
vikramaditya’s nava ratnas (nine gems) belongs to this ilk. They are named in
the following shloka:

Among these, Varahamihira was an astrologer of eminence and his book


Brihat Jataak is recokened as the ultimate authority in astrology.
He was once talking with Amarasimha,another gem among the nava ratnas
and the author of Sanskrit thesaurus, Amarakosha.
Amarasimha wanted to know the final position of a person, who starts from the
origin 0 0 and travels per following scheme.

Scheme
He first turns and travels 10 units of distance
His second turn is upward for 20 units
Third turn is to the left for 30 units
Fourth turn is the downward for 40 units
Fifth turn is to the right(again) for 50 units
… And thus he travels, every time increasing the travel distance by 10 units.

Test Cases
Case 1
Input : 3
Expected Output :-20 20
Case 2
Input: 4
Expected Output: -20 -20
Case 3
Input : 5
Expected Output : 30 -20

1
SkillFactory
Case 4
Input : 7
Expected Output : 90 -20

2
SkillFactory
Problem Statement (Word is Key) (TCS Ninja – Aug 2019 Slot 1)
One programming language has the following keywords that cannot be used
as identifiers:
break, case, continue, default, defer, else, for, func, goto, if, map, range,
return, struct, type, var
Write a program to find if the given word is a keyword or not

Test cases
Case 1
Input – defer
Expected Output – defer is a keyword
Case 2
Input – While
Expected Output – while is not a keyword

3
SkillFactory
Question. Find the nth term of the series. (TCS Ninja – Dec 2018 Slot 2)
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 ….
This series is a mixture of 2 series – all the odd terms in this series form a
geometric series and all the even terms form yet another geometric series.
Write a program to find the Nth term in the series.
The value N in a positive integer that should be read from STDIN.
The Nth term that is calculated by the program should be written to STDOUT.
Other than value of n th term,no other character / string or message should be
written to STDOUT.
For example , if N=16, the 16th term in the series is 2187, so only value 2187
should be printed to STDOUT.
You can assume that N will not exceed 30.

Test Case 1
Input- 16
Expected Output – 2187

Test Case 2
Input- 13
Expected Output – 64

Explanation
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 can represented as :
2(0), 3(0),2(1), 3(1),2(2), 3(2),2(3), 3(3),2(4), 3(4),2(5), 3(5),2(6), 3(6) ….
There are two consecutive sub GP’s at even and odd positions
(GP-1) At Odd Positions (Powers of 2) – 1, 2, 4, 8, 16, 32, 64, 128
(GP-2) At Even Positions (Powers of 3) – 1, 3, 9, 27, 81, 243, 729, 2187
Clearly, for calculating Nth position value
If N is Even, Find (N/2) position in sub GP – 2
If N is Odd, Find (N/2 + 1) position in sub GP – 1

4
SkillFactory
A washing machine works on the principle of Fuzzy System, the weight of
clothes put inside it for washing is uncertain But based on weight measured by
sensors, it decides time and water level which can be changed by menus
given on the machine control area.
For low level water, the time estimate is 25 minutes, where approximately
weight is between 2000 grams or any nonzero positive number below that.
For medium level water, the time estimate is 35 minutes, where approximately
weight is between 2001 grams and 4000 grams.
For high level water, the time estimate is 45 minutes, where approximately
weight is above 4000 grams.
Assume the capacity of machine is maximum 7000 grams Where
approximately weight is zero, time estimate is 0 minutes.
Write a function which takes a numeric weight in the range [0,7000] as input
and produces estimated time as output is: “OVERLOADED”, and for all other
inputs, the output statement is “INVALID INPUT”.
Input should be in the form of integer value –
Output must have the following format –
Time Estimated: Minutes

Example:
Input value
2000
Output value
Time Estimated: 25 minutes

5
SkillFactory
Problem Description -: Given two non-negative integers n1 and n2, where
n1<n2. The task is to find the total number of integers in the range [n1, n2]
(both inclusive) which have no repeated digits.

For example:
Suppose n1=11 and n2=15.
There is the number 11, which has repeated digits, but 12, 13, 14 and 15 have
no repeated digits. So, the output is 4.

Example1:
Input:
11 — Vlaue of n1
15 — value of n2
Output:
4

Example 2:
Input:
101 — value of n1
200 — value of n2
Output:
72

6
SkillFactory
Compute the nearest larger number by interchanging its digits [Link]
2 numbers a and b find the smallest number greater than b by interchanging
the digits of a and if not possible print -1.

Input Format
2 numbers a and b, separated by space.
Output Format
A single number greater than b.
If not possible, print -1

Constraints
1 <= a,b <= 10000000

Example 1:

Sample Input:

459 500

Sample Output:
549

Example 2:

Sample Input:

645757 457765

Sample Output:
465577

7
SkillFactory
Application ID
In an online exam the test paper set is categorized by the letters A-Z. The
students enrolled in the exam have been assigned a numeric value called
application ID. To assign the test set to the student, firstly the sum of all the
digits in the application ID is calculated. If the sum is within the numeric range
1-26 the corresponding alphabetic set code is assigned to the student, else
the sum of digits is calculated again and so on until the sum falls within the
range of 1-26.
Input 1: 6442
Output 1: P

Input 2: 558823
Output 2: D

8
SkillFactory
An e-commerce site wishes to enhance its ordering process. They plan to
implement a new scheme of OTP (One Time Password) generation for order
confirmations. The OTP can be any number of digits. For OTP generation, the
user will be asked to enter two random numbers. The first number entered
should always be smaller than the second number. The OTP is calculated as
the sum of the maximum and the minimum prime values in the range of the
user-entered numbers.
Write an algorithm to find the OTP
Input
The input consists of two space-separated integers – firstNumber and
secondNumber, representing the two numbers entered by the user. Both
numbers are considered in the range.
Output
Print an integer representing the sum of the largest and smallest prime
numbers in the range of given numbers.
Constraints
-109< firstNumber <secondNumbers<109
Example
Input:
-97 50
Output:
50
Explanation
The smallest and largest prime numbers within the given numbers are -97 and
47, respectively. The sum of -97 and 47 is 50. So, the output is 50.

9
SkillFactory
SecretMessage agency provides message encoding and decoding services
for secure data transfer. The first step in decoding includes removal of special
characters and the whitespaces from the message, as special characters and
whitespaces do not hold any meaning.
Write an algorithm to help the agency find the number of special characters
and whitespaces in a given message.
Input
The input consists of a string message, representing the message that need
to be decoded by the agency.
Output
Print an integer representing the number of special characters and
whitespaces present in a given message.
Example
Input:
gasgg54@#vscsd!s*
Output:
4
Explanation
The special characters having no meaning are ('@','#';’!’,’*'].

10
SkillFactory
A company is transmitting data to another server. The data is in the form of
numbers. To secure the data during transmission, they plan to obtain a
security key that will be sent along with the data. The security key is identified
as the count of the repeating digits in the data.
Write an algorithm to find the security key for the data.
Input
The input consists of an integer data, representing the data to be transmitted.
Output
Print an integer representing the security key for the given data. If no data is
repeated it should display
-1
Example
Input:
578378923
Output:
3

Explanation
The repeated digits in the data are 7, 8 and 3. So, the security key is 3.

11
SkillFactory
Gift Hampers to Winners
Description
The manager of a supermarket wants to organize an event in which he will
distribute gift hampers to the winners of the event. The manager has planned
in such a way that each customer has to pick the product in a pair and each
pair has different types of products. Any two customers can't pick the same
type of product pair but the price may be same. There are N types of products
and each product has a price. He will offer the gift hampers to those
customers for whom the difference of the price of the products pickedby them
is equal to the given integer value K.
Write an algorithm to help the Manager find the total numbers of lucky
customers who will get the gift hampers.

INPUT:
ProductTypes : an integer representing the types of products(N)
numK, an integer representing the given value K
prices, a list of integers representing the price of the products.
OUTPUT:
return a integer representing the total number of lucky customers who will get
the gift hamper.

Constraints
NA
Example
INPUT:
6
13
10 15 23 14 2 15

OUTPUT:
3

12
SkillFactory
An e –commerce company is planning to give a special discount on all its
product to its customers for the Christmas holiday. The company possesses
data on its stock of N product types. The data for each product type
represents the count of customers who have ordered the given product. If the
data K is positive then it shows that the product has been ordered by K
customers and is in stock. If the data K is negative then it shows that it has
been ordered by K customers but is not in stock. The company will fulfill the
order directly if the ordered product is in stock. If it is not in stock, then the
company will fulfill the order after they replenish the stock from the
warehouse. They are planning to offer a discount amount A for each product.
The discount value will be distributed to the customers who have purchased
that selected product. The discount will be distributed only if the decided
amount A con be divided by the number of orders for a particular product.
Write an algorithm for the sales team to find the number of products out of N
for which the discount will be distributed.

Input
The first line of the input consists of two space-separated integers –
numOfProducts and disAmount, representing the number of different types of
products (N) and the discount amount that will be distributed among the
customers. Order N representing the current status of the stock for the orders
of the respective product types.

Output
Print an integer representing the number of products out of N for which the
discount will be distributed.

Constraints
0 ≤ numOfProducts, disAmount ≤105
-106 ≤ order i ≤106
0≤ i ≤ numOfProducts
Example

13
SkillFactory
Input:
7 18
9 -3 8 -7 -8 18 10
Output:
2
Explanation:
The product for which the number of customers will collect the discount
amount “18” are for product types 0 and 5, i.e. 9 and 18, respectively. So, the
output is 2

14
SkillFactory
Description
A company provides network encryption for secure data transfer. The data
string is encrypted prior to transmission and gets decrypted at the receiving
end. But due to some technical error, the encrypted data is lost and the
received string is different from the original string by 1 character. Arnold, a
network administrator, is tasked with finding the character that got lost in the
network so that the bug does not harm other data that is being transferred
through the network.
Write an algorithm to help Arnold find the character that was missing at the
receiving end but present at the sending end.

Input
The input consists of two space-separated strings – stringSentandstringRec,
representing the string that was sent through the network, and the string that
was received at the receiving end of the network, respectively.

Output:
Print a character representing the character that was lost in the network during
transmission and if there is no data loss during transmission then print “NA”.

Constraints
NA

Example
Input:
abcdfjgerj abcdfijger
Output:
j
Explanation
The character ‘j’ at the end of the sent string was lost in the network during
transmission.

15
SkillFactory
You are playing an online game. In the game, a numbers is displayed on the
screen. In order to win the game, you have to Count the trailing zeros in the
factorial value of the given number. Write an algorithm to count the trailing
zeros in the factorial value of the given number.
Input
The input consists of an integer num, representing the number displayed on
the screen.
Output
Print An integer representing the count of trailing zeros in the factorial of the
given numbers.
Note: The factorial of the number is calculated as the product of integer
numbers from 1 to num.
Constraints
NA
Example
Input:
5
Output:
1
Explanation:
On calculating the factorial of 5, the output is 120 (1 x2x3x4x5). There is only
one trailing 0 in 120, So the output is 1.

16
SkillFactory
In a science research lab, the combination of two nuclear substances
produces an initial energy A. This energy A changes at a consistent rate R
every second. The energy gets multiplied by a constant value R every second.
The scientist wishes to calculate the energy produced at every second if the
reaction is allowed to happen for N seconds. Write an algorithm to find the
energy produced at every second if the reaction is allowed to happen for N
seconds.
Input
The input consists of three space-separated integers –intialEnergy, rate and
time. representing the initial energy produced on combining the nuclear
substances (A), the consistent rate of change (R), and the seconds for which
the reaction is allowed to happen (N), respectively.
Output
Print N space-separated integers representing the energy produced at every
second if the reaction is allowed to happen for N seconds.
Constraints
-106≤intialEnergy,rate ≤ 106
0 ≤ time ≤ 100
Example
Input:
533
Output:
5 15 45
Explanation:
For N =1, an initial energy of 5 is generated.
For N=2, a consistent rate of 3 is multiplied to it, so it becomes 15.
For N=3, again 3 is multiplied to the previous energy value, so it becomes 45.
So the out is 5, 15, 45.

17
SkillFactory
Given three numbers b, e and m. Fill in a function that takes these three
positive integer values and outputs b^e mod m.
Input specification:
Input1: poisitive integer, b
Input2: poisitive integer, e
Input3: poisitive integer, m
Output Specification:
Return an integer on calculating b^e mod m.
Example 1:
Input 1: 2
Input 2: 10
Input 3: 1025
Output: 1024
Explanation:
2^10 mod 1025 = 1024
Example 2:
Input 1: 4
Input 2: 10
Input 3: 1025
Output: 1
Explanation:
4^10 mod 1025 = 1

18
SkillFactory
Write a number ‘N’ (containing at most 10,000 digits), find the next grater
number having the same digits. It is guaranteed that there exists a next
greater number having the same digits as N.
Input specification:
Input1: The length of the string ‘N’.
Input2: The number ‘N’ in the form of a string.
Output Specification:
Return the next greater number having the same digits as ‘N’ in the form of a
string.
Example 1:
Input 1: 3
Input 2: 182
Output: 218
Explanation:
The next greater number after 182 is 218 using {1,8,2}
Example 2:
Input 1: 4
Input 2: 2345
Output: 2354
Explanation:
The next greater number after 2345 is 2345 using {2,3,4,5}

19
SkillFactory
Anagrams
An anagram is a word, phrase, or name formed by rearranging the letters of
another word, phrase or name.
Write a function to check if the given two strings are anagrams or not. return
“Yes” if they are anagrams otherwise return “No”.
Input specification:
Input1: The first string.
Input2: The second string.
Output Specification:
return “Yes” if they are anagrams otherwise return “No”.
Example 1:
Input 1: build
Input 2: dubli
Output: Yes
Explanation:
build and dubli are anagrams
Example 2:
Input 1: abcde
Input 2: bcdef
Output: No
Explanation:
abcde and bcdef are not anagrams

20
SkillFactory
Maximum Sum
Write a program that adds up the largest row sum and the largest column sum
from an N-rows *M-columns array numbers.
Input specification:
Input1: Integer for row dimension of the array.
Input2: Integer for column dimension of the array.
Input3: Array elements to be entered in row major.
Output Specification:
Largest row sum + Largest column sum
Example 1:
Input 1: 2
Input 2: 2
Input 3: {1,2,5,6}
Output: 19
Explanation:
Largest row(5,6) + Largest Column(2,6) = 11 + 8 =19
Example 2:
Input 1: 3
Input 2: 3
Input 3: {1,2,3,4,5,6,7,8,9}
Output: 42
Explanation:
Largest row(7,8,9) + Largest Column(3,6,9) = 24 + 18 = 42

21
SkillFactory
Accenture Coding Question | Nth Prime Number
A prime number is a whole number greater than 1 that is divisible only by itself
and the number 1. For example, 2,3,5 etc are at prime numbers as they are
divisible only by themselves and 1.
Input specification:
Input 1: An integer n.
Output Specification:
Return the nth prime.
Example 1:
Input 1: 1
Output: 2
Explanation:
2 is the first prime number.
Example 2:
Input 1: 5
Output: 11
Explanation:
Prime number series is 2,3,5,7,11,13
5th prime number is 11.

22
SkillFactory
Accenture Coding Question | Adam’s Charity
Adam decides to be generous and do some charity. starting today, from day 1
until day n, he gives i^2 coins to charity on day ‘i’ (1<=i<=n).
return the total coins he would give to charity.
Input specification:
Input 1: number of days of Charity.
Output Specification:
Return the total number of coins till charity days.
Example 1:
Input 1: 2
Output: 5
Explanation:
There are 2 days.
Example 2:
Input 1: 3
Output: 14
Explanation:
There are 3 days.

23
SkillFactory
Write a function to reverse a string word-wise.
Input specification:
Input 1: String
Output Specification:
Return the reversed string that is the last word in input string should come at
the first position of the output string second last word at he second position
and so on., individual words should remain same.
Example 1:
Input 1: Welcome to code.
Output : code to Welcome
Explanation:
Reversed string word wise
Example 2:
Input 1: Code to Crack Puzzle
Output : Puzzle Crack tp Code
Explanation:
Reversed string word wise

24
SkillFactory
Accenture Coding Question | Frequency Count
Given a string, find the frequencies of each of the characters in it.
The input string contains only lowercase letters. The output string should
contain a letter followed by its frequency, in the alphabetical order (from a to
z).
Input specification:
Input 1: The input string.
Output Specification:
Return a string representing the frequency counts of characters in the input
string.
Example 1:
Input 1: babdc
Output: a1b2c1d1
Explanation:
In the input string, ‘a’ appears once, ‘b’ appears twice, ‘c’ and ‘d’ appear once.

25
SkillFactory
Accenture Coding Question | Electrostatic Field
Doug is fond of change, every now and then he tries to do new things. This
time, he caught up with a rod comprising of negative (N) and positive (P)
charges. He is asked to calculate the maximum net electrostatic field possible
in the region due to the rod.
Note: Assume Electrostatic Field = Total charge * 100
Input specification:
Input 1: Integer array denoting the magnitude of each charge.
Input 2: String denoting nature of each charge ith represents a sign of charge
at ithentry represents a sign of charge at ith location in input1
Input 3: No of charges it holds (length of input1)
Output Specification:
Return the next maximum electrostatic field possible in the rod.
Example 1:
Input 1: {4,3,5}
Input 2: PNP
Input 3: 3
Output: 600
Explanation:
The maximum electric charge on the rod is 4-3+5 = 6 units. So the magnitude
of the electric field would be 6*100=600
Example 2:
Input 1: {2,3}
Input 2: PN
Input 3: 2
Output : 100
Explanation:
The maximum possible electric charge on the section of the rod is 2-3=-1 unit.

26
SkillFactory
Accenture Coding Question | Sum Of Divisor
Sum of divisors
Print the sum of the divisors of the integer number N.
Input specification:
Input 1: The integer ‘n’.
Output Specification:
Return the sum of divisors of ‘n’.
Example 1:
Input 1: 6
Output: 12
Explanation:
Divisors of 6 are 1,2,3,6. Sum of number (1+2+3+6)=12
Example 2:
Input 1: 36
Output: 91
Explanation:
Divisors of 36 are 1,2,3,4,6,9,12,18,36. Sum of number
(1+2+3+4+6+9+12+18+36)=91

27
SkillFactory
Array Permutation
The function accepts an integer array ‘arr’ of length ‘size’ as the argument.
Implement the function to find and return the maximum number that can be
formed by any permutation or arrangement of the digits obtained from all the
numbers present in the array. You have to return the number formed as a
string.
Note: You may need to rearrange the digits of the numbers to form the
maximum number.
Example:
Input: 34 79 58 64
Output: 98765443
Explanation:
All digits from all the numbers of the array are 3,4,7,9,5,8,6,4. Maximum
number obtained after rearranging all these digit gives 98765443.
Sample input:
21 90 23
Sample Output:
932210

28
SkillFactory
Accenture Coding Question | Turn off Bit
Turn Off Bit
The function accepts a positive integer array ‘n’ and position of the bit to be
turned off ‘k’. Implement the function to turn off the kth bit of the binary
representation of ‘n’ and return decimal equivalent of the obtained number.
Consider position of ‘k’ according to “little Endian’ notation (i.e. right to left).
Assumptions:
k>0
The number of bits of the binary representation of ‘n’ is >=k
Note:
When counting the bits from right to left, start counting from 1.
Return ‘n’, if kth bit is already off.
Example:
Input:
n: 19
k: 2
Output: 17
Explanation:
n : 19
Binary Representation of n : 10011
k:2
Binary Representation of number after turning off kth bit : 10001
return Value : 17
Sample Input:
n : 13
k:3
Sample Output :
9

29
SkillFactory
Accenture Coding Question | Sum of Uncommon Elements
The function accepts two integer arrays ‘arr’ and ‘arr2’ of sizes n and m
respectively as its argument. Implement the function to find and return the
sum of all uncommon elements in two arrays (elements which are present in
only one of the array).
Note:
Return -1 if both arrays are null (None in the case of Python).
If one of the arrays is null then return the sum of all elements of the other
array.
Example 1:
Input 1: 9 -4 3 2 -5
Input 2: 2 -5 7 9
Output: 6
Explanation:
Uncommon elements of two arrays are -4,3 and 7. Sum of uncommon
elements = -4 + 3 + 7 = 6. Thus, output is 6.

30
SkillFactory
Accenture Coding Question | Superior Array Element
Superior Array Element
In an array, a superior element is one that is greater than all elements to its
right. the rightmost element will always be considered as a superior element.
Here, the function accepts an integer array ‘arr’ and its length ‘n’. Implement
the function to find and return the number of superior element in the array ‘arr’.
Assumptions:
1. n>0
2. Array index starts from 0.
Example 1:
Input 1: 7 9 5 2 8 7
Output: 3
Explanation:
9 is greater than all the elements to its right, 8 is greater than the element to
its right, and 7 is the rightmost element. hence total 3 superior elements.
Sample Input:
289742
Sample Output:
4

31
SkillFactory
Accenture Coding Question | Next Letter
The function accepts two characters ‘ch1’ and ‘ch2’ as the argument. ‘ch1’ and
‘ch2’ are alphabetical letters. Implement the function to find and return the
next letter so that distance between ch1 and ch2. While counting distance if
you exceed the letter ‘z’ then count the remaining distance starting from the
letter ‘a’.
Distance between two letters in the number of letters between them.
Assumptions:
All input and output characters are lower case alphabets.
Example 1:
Input 1: c
Input 2: g
Output: k
Explanation:
The distance between the letter ‘c’ and ‘g’ is 3 (d,e,f). The next letter with
distance 3 from letter ‘g’ is ‘k’. Thus the output is k.
Sample Input:
Input 1: r
Input 2: l
Sample Output:
f

32

You might also like