TCS NQT Practise Questions
TCS NQT Practise Questions
TCS NQT Practise Questions
Language
Topic
Arrays
Subtopic Arrays
Type
Coding
Solution
#include<stdio.h>
#define bool int
#define R 6
#define C 5
{
max_of_s = S[i][j];
max_i = i;
max_j = j;
}
}
}
/* UTILITY FUNCTIONS */
/* Function to get minimum of three values */
int min (int a, int b, int c)
{
int m = a;
if (m > b)
m = b;
if (m > c)
m = c;
return m;
}
printMaxSubSquare (M);
getchar ();
}
Question Number
Output 1 1 1 1
1 1 1
1 1 1
Difficulty Hard
Explanation
Question Number 2
Topic Arrays
Subtopic Arrays
Type Coding
Question
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.
def prime(n):
count = 0
for i in range(2, 99999):
flag=0
for j in range(2,i):
if (i % j == 0):
flag=1
break
if flag == 0:
count+=1
if count == n:
print(i)
break
# main
n = int(input())
if n%2 == 1:
fibbo(n//2+1)
else:
prime(n//2)
Input 1 14
Output 1 17
Difficulty Medium
Explanation When N = 14, the 14th term in the series is 17. So only the value 17 should be
printed
Question Number
Language
Topic
Arrays
Subtopic Arrays
Type
Coding
Question Number
Question Selection sorting is a simple sorting algorithm. This sorting algorithm is an in-
place comparison-based algorithm in which the list is divided into two parts,
the sorted part at the left end and the unsorted part at the right end. Initially,
the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with
the leftmost element, and that element becomes a part of the sorted array.
the right.
This algorithm is not suitable for large data sets as its average and worst
For the first position in the sorted list, the whole list is scanned sequentially.
The first position where 14 is stored presently, we search the whole list and
Question Number
10 is placed at 0th index of array, and we increase the value of loop by +1.
Then we found out that 14 is 2nd lowest element after searching the whole
array starting from 1st position of length-1 position
The same process is applied to the rest of the items in the array.
Solution
N = int(input()) # 5
A = [int(value) for value in
input().split(' ', N)]
#[64, 25, 12, 22, 11]
Input 1 5
64 25 12 22 11
Output 1 11 12 22 25 64
Question Number
Difficulty Medium
Explanation
Question Number 2
Topic Arrays
Subtopic Arrays
Type Coding
occurrences of x in arr[].
# Driver code
N = int(input())
arr = [int(value) for value in
input().split(' ', N)]
#arr = [1, 2, 2, 2, 2, 3, 4, 7 ,8 ,8]
x = int(input())
print (countOccurrences(arr, N, x))
Input 1 5
12223
2
Output 1 3
Difficulty Medium
Explanation
Question Number
Language
Topic
Strings
Type
Coding
Question
Longest Common Subsequence –
Let us discuss the Longest Common Subsequence (LCS) problem as one more
LCS Problem Statement: Given two sequences, find the length of the longest
same relative order, but not necessarily contiguous. For example, “abc”, “abg”, “bdf”,
“aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg”. So a string of length n has 2^n
It is a classic computer science problem, the basis of diff(a file comparison program
that outputs the differences between two files), and has applications in bioinformatics.
Question Number
Solution
Input 1 ABCDGH
AEDFHR
Output 1 3
Difficulty Hard
Explanation
Question Number 2
Topic Strings
Subtopic Strings
Type Coding
Question Given a string as an input. We need to write a program that will print all non-
empty substrings of that given string.
Output 1 A
B
C
AB
BC
ABC
Difficulty Medium
Explanation
Question Number
Language
Topic
Arrays
Subtopic Arrays
Type
Coding
Question Given an array which may contain duplicates, print all elements and their frequencies.
Question Number
Solution
#include <stdio.h>
int main()
{
int arr[100], freq[100];
int size, i, j, count;
freq[i] = count;
}
}
/*
* Print frequency of each element
*/
printf(“\nFrequency of all elements of array : \n“);
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf(“%d occurs %d times\n“, arr[i],
freq[i]);
}
}
return 0;
}
Output 1
10 3
20 4
51
Input 2
10, 20, 20
Question Number
Output 2
10 1
20 2
Difficulty Medium
Type Coding
Question A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit,
generate all Pythagorean Triples with values smaller than the given limit.
Question Number 2
// Driver Code
int main ()
{
int limit = 20;
pythagoreanTriplets (limit);
return 0;
}
Input 1 20
Output 1 3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
Question Number 2
Difficulty Hard
Explanation A Simple Solution is to generate these triplets smaller than the given limit using three nested
loops. For every triplet, check if the Pythagorean condition is true, if true, then print the
triplet. Time complexity of this solution is O(limit3) where ‘limit’ is given limit.
An Efficient Solution can print all triplets in O(k) time where k is the number of triplets
printed. The idea is to use square sum relation of Pythagorean triplet, i.e., addition of squares
of a and b is equal to square of c, we can write these number in terms of m and n such that,
a = m2 - n2
b=2*m*n
c = m2 + n2
because,
a2 = m4 + n4 – 2 * m2 * n2
b2 = 4 * m2 * n2
c2 = m4 + n4 + 2* m2 * n2
We can see that a2 + b2 = c2, so instead of iterating for a, b and c we can iterate for m and n
Language
Topic
Control Statements
Type
Coding
Solution
def primeSum(l, r) :
sum = 0
for i in range(r, (l - 1), -1) :
isPrime = checkPrime(i)
if (isPrime) :
sum += i
return sum
if __name__ == '__main__' :
l, r = (int(value) for value in
input().split())
print(primeSum(l, r))
Input 1 16
Output 1 10
Question Number
Input 2 4 13
Output 2 36
Difficulty Medium
Topic Array
Type Coding
Question Your birthday is coming soon and one of your friends, Alex, is thinking about a
gift for you. He knows that you really like integer arrays with interesting
properties.
He selected two numbers, N and K and decided to write down on paper all
integer arrays of length K (in form a[1], a[2], …, a[K]), where every number a[i] is
in range from 1 to N, and, moreover, a[i+1] is divisible by a[i] (where 1 < i <= K),
n = int(input())
k = int(input())
print(count(n, k))
Input 1 2
1
Output 1 2
Input 2 3
2
Question Number 2
Output 2 5
Difficulty Medium
Explanation All possible arrays are [1, 1], [1, 2], [1, 3], [2, 2], [3, 3].