You are on page 1of 17

Coding

TCS Previous Questions


Question#1
Consider the following series:
 
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.


Question#1
Consider the following series:
 
1,1,2,3,4,9,8,27,16,81,32,243,64,729,128,2187…
Series #1 1 2 4 8 16 32 Relation
Series #2 1 3 9 27 81 243
1 2 3 4 5 6 7 8 9 10

1 1 2 3 4 9 8 27 16 81
0 0 1 1 2 2 3 3 4 4
Question#1
Consider the following series:
 
1,1,2,3,4,9,8,27,16,81,32,243,64,729,128,2187…
Series #1 1 2 4 8 16 32
Series #2 1 3 9 27 81 243
1 2 3 4 5 6 7 8 9 10

0 0 1 1 2 2 3 3 4 4
1 1 2 3 4 9 8 27 16 81
k=(i+1)/2 (1+1)/2 2/2 2 2 3 3 4 4
k-1 1-1 = 0 1-1=0 1 1 2 2 3 3
Question#1
Solution #1
n=int(input("enter the number of series to print:"))
k=0
j=0
for i in range(n):
if(i%2==0):
m=2**j
j=j+1
else:
m=3**k
k=k+1
#if(i!=n-1):
# print(m,end=",")
#else:
# print(m)
print(m)
 
Question#1
Solution #2
n=int(input("enter the nth term to print:"))
if n % 2 == 0:
n //= 2
print(3 ** (n - 1))
# If input number is odd
else:
n = (n // 2) + 1
print(2 ** (n - 1))  
Question#2
Program 2:
Given an array of integers, sort the first half of
the array in ascending order and second
half in descending order.
Examples:
Input : arr[] = {5, 2, 4, 7, 9, 3, 1, 6, 8}
Output : arr[] = {1, 2, 3, 4, 9, 8, 7, 6, 5}
Input : arr[] = {1, 2, 3, 4, 5, 6}
Output : arr[] = {1, 2, 3, 6, 5, 4}
Question#2
n=int(input("Enter the size of the array"))
list1=[]
for i in range(n):
ele=int(input())
list1.append(ele)
print(list1)
list1=sorted(list1)
print(list1)
list2=list1[0:n//2]
list3=list1[n//2:n]
print(list2)
print(list3)
list4=list3[::-1]
print(list2+list4)
Question#3
Find the common elements form Given two
integers arrays
Examples:
Input : arr1[] = {5, 2,7, 9, 3, 1, 6, 8}
arr2[] = {1, 3, 4, 9, 7, 6, 5}
Output : {1,3,5,6,7,9}
Question#3
list1 = []
n = int(input("Enter number of elements in array#1: ")) # number of elements as input 1
for i in range(0, n):
ele = int(input())
list1.append(ele) # adding the element
print(list1)

list2 = []
k = int(input("Enter number of elements in array#2: ")) # number of elements as input 2
for i in range(0, k): # iterating till the range
ele = int(input())
list2.append(ele) # adding the element
print(list2)
list3=[]
for num in list1:
if num in list2:
list3.append(num)
print("Common Elements are ",list3)
Question#4

Remove duplicates character from a given string.


Input String:
engineering
Output String:
Engier
Question#4
Solution#1
str1=input("enter the string")
str2=''
for n in str1:
if n not in str2:
str2=str2+n
print(str2)
Question#4
Solution#2
str1=input("enter the string")
list1=[ ]
for n in str1:
if n not in list1:
list1.append(n)

print(list1)
str2=''.join(list1)
print(str2)
Question#4
Solution#3
str1=input("enter the string")
list1=[ ]
for n in str1:
if n not in list1:
list1.append(n)

print(list1)
str2=''.join(list1)
print(str2)
Assignment #1
String functions
List funtions
Assignment #2
Consider the below series :
• 0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8
This series is a mixture of 2 series all the odd
terms in this series form even numbers in
ascending order and every even terms is derived
from the previous term using the formula (x/2)
Write a program to find the nth term in this
series.
Assignment #3
Consider the below series :
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, ...
 
This series is a mixture of 2 series - all the odd terms
in this series form a Fibonacci series and all the
even terms are the prime numbers in ascending
order.
 
Write a program to find the Nth term in this series.

You might also like