0% found this document useful (1 vote)
472 views

PTCS Coding Questions Practice

The document contains 9 coding problems with sample inputs, outputs and explanations. The problems cover a range of concepts including string manipulation, palindrome checking, knapsack problem, finding missing letters in a sentence, counting numbers without repeated digits within a range, and correcting numbers entered in reverse order. Solutions are provided in Python for each problem.

Uploaded by

Sa Dik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
472 views

PTCS Coding Questions Practice

The document contains 9 coding problems with sample inputs, outputs and explanations. The problems cover a range of concepts including string manipulation, palindrome checking, knapsack problem, finding missing letters in a sentence, counting numbers without repeated digits within a range, and correcting numbers entered in reverse order. Solutions are provided in Python for each problem.

Uploaded by

Sa Dik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

TCS Coding Questions (Practice)

Code1:
problem Statement
Gorge was doing some typing tasks over the laptop. His daughter Kenny came suddenly and
pressed random keys on the keyboard. Unfortunately, numeric characters and some special
characters have been entered in it. Write a program to help Gorge to remove numeric
characters and some special characters from the string.

Sample Input
abcd3h46h2kk26lw$%

Sample Output
abcdhhkklw

Explanation
abcd3h46h2kk26lw$% string contains numbers and special characters. After removing of special characters
and numbers, string would look like “abcdhhkklw”. Hence output will be “abcdhhkklw”

Answer:-
PYTHON

str = input()
for i in range(0, len(str)):
if str[i] >= 'a' and str[i] <= 'z':
print(str[i],end='')

OR
s=input()
f=filter(str.isalpha,s)
s1="".join(f)
print(s1)
Code2:
Problem Statement
Jack has given a word to its student to check whether it is palindrome. But there is catch, if the word is not
palindrome, they will get 1 chance to swap 2 alphabets to make it palindrome. After 1 swap, if the word becomes
palindrome, then the student wins, otherwise they lose. Create a program that return True, if just 1 swap makes
the word palindrome, otherwise False.

Sample Input
abab

Sample Output
True

Explanation
The input word is abab. If the first and second alphabet swap each other, then it makes the word as baab, which
ultimately makes it palindrome.So the result is True.

Answer:-
PYTHON
def isPalindromePossible(input: str) -> bool:
length = len(input)
diffCount = 0
i = 0

diff = [['0'] * 2] * 2

while i < length // 2:

if input[i] != input[length - i - 1]:

if diffCount == 2:
return False

diff[diffCount][0] = input[i]

diff[diffCount][1] = input[length - i - 1]
diffCount += 1
i += 1
if diffCount == 0:
return True

elif diffCount == 1:
midChar = input[i]

if length % 2 != 0 and (diff[0][0] == midChar


or diff[0][1] == midChar):
return True

elif diffCount == 2:

if (diff[0][0] == diff[1][0] and diff[0][1] ==


diff[1][1]) or (
diff[0][0] == diff[1][1] and diff[0][1] ==
diff[1][0]):
return True
return False

str = input()
print(isPalindromePossible(str)
Code3:
Problem Statement
Given a positive integer number N. the task is to find the number of ways in which the given
number can be represented as the product of two distinct numbers.

Sample Input
14

Sample Output
2

Explanation
14 can be represented as 1 14 and 2 7

Answer:-
PYTHON

import math
n = int(input())
count = 0
for i in range (1, int(math.sqrt(n))):
if n % i == 0 and n//i != i:
count = count + 1
print(count)

OR

def countWays(n):

count = 0

i =1
while ((i * i)<n) :

if(n % i == 0):

count += 1

i += 1
return count

n = int(input())

print (countWays(n))
Code4:
Problem Statement
A group of friends are playing cards. Only numeric cards are being used, along with the joker card (equivalent to
0) and the symbols in the cards are not taken into account. Each player will receive a set of cards. The rule of
the game is to rearrange the set of cards to the possible lowest number sequence. The player with the set of
cards forming the smallest number wins the game. The number sequence of the cards should not start with a
joker card. The task is to write a program for developing the logic of the game considering the set of cards as a
number. The output will be the lowest possible number that can be formed, not beginning with 0.

Sample Input
34201

Sample Output
10234

Explanation
The lowest number formation by rearranging the digits of the number 34201 and not starting with a 0 will be 10234.
Hence the output is 10234

Answer:-
PYTHON

def smallest(lst):

for i, n in enumerate(lst):
if n != '0':
tmp = lst.pop(i)
break
return str(tmp) + ''.join(lst)

if __name__ == '__main__':
n = int(input())
lst = list(str(n))
lst.sort()
print(smallest(lst))

OR
def rearrage(n):

n = ''.join(sorted(n)) n = ''.join(a)

i=0 return n

while(n[i] == '0'): n = int(input())

i += 1 print(rearrage(str(n)))

a = list(n)

temp = a[0]

a[0] = a[i]

a[i] = temp
Code5:
Problem Statement
Mr. Rao is relocating from place A to B. the moving truck has a maximum capacity C. there are N
items in the house where each item has a corresponding value (vi) and weight (Wi). Mr. Rao has to
carry only the most valuable items whose total weight does not exceed the capacity of the truck. The
task here is to find those items (single or combination of items) whose total value (v) will be the
maximum and their corresponding weight (w) will not exceed truck capacity ( C ) here
N = no. of items
C – maximum capacity of the truck, an integer value
W0 to N-1 – An array consisting weight of each item
V0 to N-1 – An array consisting value of each item.

Sample Input
4 -> Value of N
80 -> Value of C
10 45 60 90 -> Elements of array V[]
15 20 30 40 -> Elements of array W[]

Sample Output
150

Answer:-
PYTHON

def knapSack(W, wt, val, n):

if n == 0 or W == 0:
return 0

if (wt[n-1] > W):


return knapSack(W, wt, val, n-1)
else:
return max(
val[n-1] + knapSack(
W-wt[n-1], wt, val, n-1),
knapSack(W, wt, val, n-1))

N = int(input())
C = int(input())

val = list(map(int, input().strip().split()))


Wt = list(map(int, input().strip().split()))
print (knapSack(C, Wt, val, N))
Code6:
Problem Statement
Consider the series below:
{2, 1, 3, 4, 7, 11, 18, so on}
In the above series first two elements are 2 and 1 respectively and other elements are the sum of its
twoimmediately previous elements. The task is to find the minimum number of elements that need to
be changedin the given array Arr to make a series as per the above statement.
For example:
Arr[] = {2, 1, 3, 4, 9, 11, 18}
In the given array 5th element (9) needs to replace with 7 to make the correct series. This means there
is onlyone element that needs to replace in the given array. Hence the output = 1;

Sample Input
7
2 1 3 4 9 11 18

Sample Output
1

Explanation
In the given array 5th element (9) needs to replace with 7 to make the correct series. This means there
is onlyone element that needs to replace in the given array. Hence the output = 1;

Answer:-
PYTHON

n = int(input())
arr = list(map(int, input().strip().split()))
a = 2
b = 1
count = 0
if arr[0] != a:
count = count + 1
if arr[1] != b:
count = count + 1
for i in range(2, n):
c = a + b
a = b
b = c
if arr[i] != c:
count = count + 1
print(count)
Code7:
Problem Statement
Given a sentence str. The task is to find whether the given sentence contains all letters of the
English alphabet(a to z or A to Z). If it does not, then print all missing letters of the alphabet,
otherwise print 0.
Input Format
Input contains a sentence containing english alphabets.
Output Format
Output should print the missing alphabets.
Note
All characters in the given sentence should be treated as case insensitive, which means that
‘A’ and ‘a’ are tobe treated as the same.
The output should be in alphabetic order
The output should contain only lowercase alphabets
If none of the letters are missing, print 0 as output

Sample Input
The four boxing wizard starts over the quickly.

Sample Output
Jmp

Explanation
“The four boxing wizard starts over the quickly” does not contain all the letters from ‘a’ to ‘z’ or
‘A’ to ‘Z’ as ‘j’, ‘m’and ‘p’ are missing.

Answer:-
PYTHON

str = input()
str = str.lower()
for i in range (0, 26):
t = chr(97+i)
if t not in str:
print (t, end='')
Code8:
Problem Statement
Given two non-negative integers n1 and n2, where n1 < n2. The task is to find the total
number of integers in the range interval 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.

Sample Input
11
15

Sample Output
4

Answer:-
PYTHON

def repeated_digit(n):
a = []

while n != 0:
d = n % 10
if d in a:
return 0
a.append(d)
n = n // 10
return 1

def calculate(L, R):


answer = 0
for i in range(L, R + 1):
answer = answer + repeated_digit(i)
return answer

L = int(input())
R = int(input())
print(calculate(L, R))
Code9:
Problem Statement
An accountant in a firm has a serious issue with numbers. He types the numbers in a reverse manner.
Supposehe has to enter 123, he enters the number 321. He has the habit of reading numbers from
right to left. The bossbecame aware of this only after he found out at the end of the billing month when
he had to file the tax. He hasto correct al the numbers by reentering each number from right to left.
This gives the corrected number.
Input Format
Given a number N, help the boss find out the corrected numbers.
Output Format
Display the corrected numbers as output. Also ignore any 0’s at the end of the number.
Note The corrected numbers should be only 16-bit signed integers. If the output (the corrected number
isoutside the range display “Wrong value”.

Sample Input
-4512

Sample Output
-2154

Explanation
From the inputs given belowN = -4512
The correct number would be from right to left which is 2154 and the sign is retained. Hence the output
is -2154

Answer:-
PYTHON

n = int(input())
temp = n
if (n >= -32786 and n <= 32767):
if(n < 0):
n = n * -1
rev = 0
while(n != 0):
rem = n % 10
rev = rev * 10 + rem
n = n // 10
if(temp < 0):
print(rev * -1 )
else:
print(rev)
else:
print("Wrong value")
Code10:
Problem Statement
Ria likes to play number games always. Today she decided to find the largest number that can be made usingall
of the digits of the given input (Non-negative integer) value N
Input Format
Input contains an integer N.
Output Format
Output contains the largest number formed with the input N.

Sample Input:
3675092

Sample Output
9765320

Answer:-
PYTHON
def printMaxNum(num):

hash = []
for i in range(0, 10):
hash.append(0)

if(num < 0):


n = num * -1
else:
n = num

ans = 0
while (n != 0):
hash[int(n % 10)] = hash[int(n % 10)] + 1
n = n // 10

if (num > 0):


for i in range(9, -1, -1):
for j in range(0, hash[i]):
ans = ans * 10 + i
else:
if (hash[0] > 0):
for i in range(1, 10):
if (hash[i] > 0):
ans = i
hash[i] = hash[i]-1
break

for i in range(0, 10):


for j in range(0, hash[i]):
ans = ans * 10 + i
ans = ans * -1
return ans

N = int(input())
print(printMaxNum(N))

You might also like