TCS NQT Practise Questions
TCS NQT Practise Questions
TCS NQT Practise Questions
Topic Arrays
Subtopic Arrays
Type Coding
Solution #include<stdio.h>
#define bool int
#define R 6
#define C 5
/* 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 ();
}
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 1
Topic Arrays
Subtopic Arrays
Type Coding
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
find that 10 is the lowest value.
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.
Input 1 5
64 25 12 22 11
Output 1 11 12 22 25 64
Difficulty Medium
Explanation
Question Number 2
Topic Arrays
Subtopic Arrays
Type Coding
# 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 1
Topic Strings
Type Coding
Question
Longest Common Subsequence –
Let us discuss the Longest Common Subsequence (LCS) problem as
Programming.
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.
Input 1 ABC
Output 1 A
B
C
AB
BC
ABC
Difficulty Medium
Explanation
Question Number 1
Topic Arrays
Subtopic Arrays
Type Coding
Question Given an array which may contain duplicates, print all elements and their
frequencies.
Solution
#include <stdio.h>
int main()
{
int arr[100], freq[100];
int size, i, j, 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
Output 2 10 1
20 2
Difficulty Medium
Type Coding
Input 1 20
Output 1 3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
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
An Efficient Solution can print all triplets in O(k) time where k is the
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
Type Coding
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
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
Output 2 5
Difficulty Medium
Explanation All possible arrays are [1, 1], [1, 2], [1, 3], [2, 2], [3, 3].