0% found this document useful (0 votes)
49 views9 pages

TCS NQT Coding Previous Q's

The document contains a list of coding questions and programming tasks designed for TCS NQT preparation, covering various topics such as string manipulation, linked lists, sorting algorithms, and matrix operations. Each question includes input and output examples to illustrate the expected results. The document serves as a resource for practicing coding skills and understanding fundamental programming concepts.

Uploaded by

Vasudha J
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 (0 votes)
49 views9 pages

TCS NQT Coding Previous Q's

The document contains a list of coding questions and programming tasks designed for TCS NQT preparation, covering various topics such as string manipulation, linked lists, sorting algorithms, and matrix operations. Each question includes input and output examples to illustrate the expected results. The document serves as a resource for practicing coding skills and understanding fundamental programming concepts.

Uploaded by

Vasudha J
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

t.

me/aptitudehemanth

TCS NQT CODING QUESTIONS

Set 1 – Programs

1) Test whether an input string of opening and closing parentheses was balanced or not.
If yes , return the no. of parentheses. If no, return -1.
Ip: “(( ))”
Op: 2
Ip: “()(“
Op:-1

2) Reverse the second half of an input linked list. If the input linked list contained odd
number of elements , consider the middlemost element too in the second half.

3) Write a program to Merge sort using pointers

4) Merge two sorted singly linked lists into one sorted list

5) Reverse a linked list using recursion and without recursion

6) Removal of vowel from string

7) Print and count all the numbers which are less than a given key element from a
given array.

8) Eliminate repeated letters in Array. Printing non repeating elements in array and
printing the total.
Arr1 = {1,2,3,4,5} Len1 = 5
Arr 2 = {2,6,8,10} Len2 = 4
Output = {1,3,4,5,6,8,10} Total is 7
t.me/aptitudehemanth

9) String Reversal

10) Find a target value in a two-dimensional matrix given the number of rows as row
Count and number of columns as column Count, and return its coordinates. If the value
didn't exist, the program had to return (-1,-1).

11) Input: num1 and num2 such that 0 <= num1 <= 99999999 and 0 <= num2 <= 9. You
have to find number of occurences of input num2 in input num1 and return it with
function int isOccured(int num1, int num2).
Example:
Input: num1= 199294, num2= 0
Output: 3
Test Case 1:
Input:
1222212
2
Expected Output:
5
Test Case 2:
Input:
1001010
0
Expected Output:
4

12) Find a sub string in a given string and replace it with another string?

13) Remove all the vowels from a given string using pointers concept Ip:
{a,b,c,d,e,f,g,h,I,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}
Op: Need to use function call, return value to main function, Pass it as argument

14) Program to check for prime number:


t.me/aptitudehemanth

15) To find GCD of two number.

16) Find the GCD of N numbers using pointers

17) Print all the prime numbers which are below the given number separated by
comma.
Input: 3
Output: 2,3
Ip: 11
Output: 2,3,5,7

18) Get an unsorted array and convert into alternate array(alternate arrayascending
order array by taking alternate elements).Half elements of array in ascending remaining
half in descending.The first n elements should be sorted in ascending order and the next
part should be sorted in descending and print it
Test Case Input: [1,2,3,4,5,6,7,8],8,4
Output: {1,2,3,4,8,7,6,5}
Function: fn_name(input array, length of array, no of digits to sort (here 3))
Fn_name(input array, 8,4)

19) Given 5,1,4,7,9 do alternate sort (odd position sorting) for this..and print 4,5,9

20) Find the occurence of a substring in a parent string


Input : aAbcDefabcAdf
Substring : abc
Output : 1

21) Write a program to rotate a matrix in 90 degree.


Case 1: Flag = 1
Input: 2 3 1
463
542
Output: 5 4 2
463
231
Case 2: Flag = 0
t.me/aptitudehemanth

Input: 2 1
34
Output: 4 1
32

22) Matrix Transposition


Input
123
467
8 9 11
Output
841
962
11 7 3
Input:
123
456
789

Output:
147
258
369

23) Transpose the given matrix, multiply it with the given matrix and print the transpose
of the result.

24) Pattern Printing


Input: n=3
333
313
323
333
Input: n=4
4444
4414
t.me/aptitudehemanth

4424
4434

25) Write a program to print numbers in the order of their frequency of occurrence.
Ip: {1,1,2,2,2,3,4,4,5,5,5,5,5}. Length - 13
Op: {5,5,5,5,5,2,2,2,1,1,4,4,3}
Given Function: int *fn_name(int *arr, int length of array}

26) Write a program to print numbers in the order of their frequency of occurrence.
Ip: {1,22,333,44,5555,7,8,9,10}. Length - 10
Op: {5555,333,22,44,1,7,8,10}
Given Function: int *fn_name(int *arr, int length of array}
Use malloc() function to get the array

27) Program to rotate a given string. If 2 strings are equal then print 1 else print -1.
Given 2 strings.
Ip: sample
Mplesa
Op: 1
Ip: sample
Mplase
Op:-1
Ip: abc
cba
Op: 1
Ip: ab
Aa
Op: -1

28) Write a program to print the not common numbers in both the arrays.
Input: Arr1 = {1,2,3,4}
Arr2= {4,5,6,7}
Length = 4
Length = 4
Output: 6
t.me/aptitudehemanth

29) Write a program to remove duplicate elements in a array


Ip: {1,1,1,1,2,2,2,3,3,4,5}
Op: {1,2,3,4,}
Given Function: int *fn_name(int *arr, int length of array}
Use malloc() function to get the input array

30) Copying value to its corresponding index (swap values and index in a array).
Input: a[0] = 2; a[1] = 0; a[2] = 3; a[3] = 1; a[4] = 5; a[5] = 4;
Output: a[0] = 1; a[1] = 3; a[2] = 0; a[3] = 2; a[4] = 5; a[5] = 4;

31) Write a program to sort numbers in a array and print the numbers with Balternate
digits. Use malloc() to get the input array.
Ip: {4,2,5,3,7}
Op: {2,4,7}

32) Pattern Question:- For n=5


1
3*2
4*5*6
10*9*8*7
11*12*13*14*15

33) Pattern Question:- For n=5


1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
t.me/aptitudehemanth

34) Pattern Question:-


To print the pattern like for n=3 the program should print
1112
3222
3334
print n=6; n stands for number of lines
1111112
3222222
3333334
5444444
5555556
7666666

35) To print the trapezium pattern. for example , we have n=4 the output
should be like
1*2*3*4*17*18*19*20
- -5*6*7*14*15*16
- - - -8*9*12*13
- -------- 10*11

Coding – 3rd August (NQT)


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
Sample
Input1: defer
Output1: defer is a keyword
Input2: While
Output2: While is not a keyword
t.me/aptitudehemanth

Sol-

key = {'break', 'case', 'continue', 'default', 'defer', 'else', 'for', 'func', 'goto', 'if', 'map',
'range',
'return', 'struct', 'type', 'var'}
instr = input()
if instr in key:
print(instr + " is a key")
else:
print(instr + " is not a key")

2. Given a maximum of 100 digit numbers as input, find the difference between the sum
of odd and even position digits.

Input #1: 4567


Expected Output: 2
Explanation:
Sum of odd position digits 4 and 6 is 10. Sum of even position digits 5 and 7 is 12. The
difference is 12 – 10 = 2.
Input #2: 9834698765123
Expected Output: 1
Input #3: 5476
Expected Output: 2

def digit(num):
X = num
digi = []
while(X>0):
digi.append(X%10)
X = X//10
return digi
num = int(input())
digi = digit(num)
By – XYZ2
odd = 0
even = 0
for i in range(len(digi)):
if (i%2==0):
odd+=digi[i]
else:
even+=digi[i]
print(abs(odd-even))

3. Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G}


as
input, output its decimal value.
Input1: 1A
Output1: 27
Input2: 23GF
Output2: 10980

num = input()
base17 = {'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15, 'G':16}
n=int(len(num)-1)
dec = 0
for i in num:
if i.upper() in base17:
dec+=base17[i]*17**n
else:
dec+=int(i)*17**n
n-=1
print(dec)

You might also like