Table of Contents
In this article, I will share TCS NQT 2024 question asked daywise.
TCS NQT Coding Question 26 April 2024
Question 1 : 26 April 2024
The device ‘ABC’ is designed to Move on a rectangle grid with M rows & N column. The ABC is initially placed at (1,1) i.e. In the upper left cell. The device must enter the (M,N) grid cell. At one point the device can only move to cells immediately east & South of it. This means that if the device is currently at (i,j) it can move to either the (i + 1, j) or (i, j + 1) cell, as long as device does not leave the grid. Now same one has placed several the placed several obstacles in random places on the grid that the device cannot get through. Give the location of the blocked cells, The task is to calculate how many path can travel from (1,1) to (M, N).
Explanation
Input
M=3
N=3
Obstacle =[(2,2)]
Output
2
Code Implementation
#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
int f(int i, int j, vector<vector<int>> &dp){
if(i == 0 && j == 0) return 1;
if(i < 0 || j < 0) return 0;
if(dp[i][j] != -1) return dp[i][j];
int up = f(i-1, j, dp);
int left = f(i, j-1, dp);
return dp[i][j] = up + left;
}
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp (m, vector<int>(n, -1));
return f(m-1, n-1, dp);
}
};
Question 2 : 26 April 2024
Meet Ananya, data analyst at retail company called ‘ABC’ corpration Ananya’s manager, Jatin has provide her with an inventory stock report in the from of an array of intergers. Negative for the past month. Anaya’s objective is to find the number of sub-array with in the data whose sum of elements matches a a specific target sum.
Explanation
Input
Arr = {3, 4, -7, 1, 3, 3, 1, -4}
Sum =7
Output
4
[0 … 1 ] — {3,4}
[0 … 5] — {3,4,-7,1,3,3}
[3 … 5] — {1,3,3}
[4 … 6] — {3,3,1}
The target sum is 7, hence therefore 4 sub-array which having target sum equal to 7.
Code Implementation
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
void bruteForce(vector<int>& arr, int n, int target) {
for (int i = 0; i < n; i++) {
int curSum = 0;
for (int j = i; j < n; j++) {
curSum += arr[j];
if (curSum == target) {
for (int k = i; k <= j; k++) {
cout << arr[k] << " ";
}
cout << endl;
}
}
}
}
void optimalApproach(vector<int>& arr, int n, int target) {
unordered_map<int, int> sumMap;
int curSum = 0;
for (int i = 0; i < n; i++) {
curSum += arr[i];
if (curSum == target) {
for (int j = 0; j <= i; j++) {
cout << arr[j] << " ";
}
cout << endl;
}
if (sumMap.find(curSum - target) != sumMap.end()) {
int startIndex = sumMap[curSum - target] + 1;
for (int k = startIndex; k <= i; k++) {
cout << arr[k] << " ";
}
cout << endl;
}
sumMap[curSum] = i;
}
}
int main() {
vector<int> arr = {3, 4, -7, 1, 3, 3, 1, -4};
int N = arr.size();
int target = 7;
optimalApproach(arr, N, target);
return 0;
}
Question 3 : 26 April 2024
Given two Integer, find the sum of all cubes such that all numbers are in the range of n and m.
Sample Input:
n=4
m=9
Sample Output:
1989
Code Implementation
#include <iostream>
using namespace std;
void findCubeSum(int start, int end) {
int cubeSum = 0;
for (int i = start; i <= end; i++) {
cubeSum += i * i * i;
}
cout << cubeSum << endl;
}
int main() {
int start, end;
cin >> start >> end;
findCubeSum(start, end);
return 0;
}
TCS NQT Coding Question 29 April 2024
Question 1 : 29 April 2024
Given an integer, we need to find the sum of values of that table.
Input
10
Output
550
Explanation
10*1+10*2+..10*10
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int mSum = 0;
for (int i = 1; i <= n; i++) {
mSum += n * i;
}
cout << mSum << endl;
return 0;
}
Question 2 : 29 April 2024
Given an array and a integer k.we need to find the maximum element in each of the contiguous subarrays.
Input
2 4 7 1 6 3
K=3
Output
7 7 7 6
Explanation
The subarrays will be [2,4,7], [4,7,1] [7,1,6] and [1,6,3]. The maximum numbers from the subarrays are 7 7 7 6.
Code Implementation
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
void solve(vector<int>& arr, int k) {
vector<int> ans;
for (int i = 0; i < arr.size() - k + 1; i++) {
priority_queue<int> maxHeap;
for (int j = i; j < i + k; j++) {
maxHeap.push(arr[j]);
}
ans.push_back(maxHeap.top());
}
for (int num : ans) {
cout << num << " ";
}
cout << endl;
}
void optimalSolution(vector<int>& arr, int k) {
priority_queue<pair<int, int>> maxHeap;
vector<int> ans;
for (int i = 0; i < k; i++) {
maxHeap.push({arr[i], i});
}
ans.push_back(maxHeap.top().first);
for (int i = k; i < arr.size(); i++) {
maxHeap.push({arr[i], i});
while (maxHeap.top().second <= i - k) {
maxHeap.pop();
}
ans.push_back(maxHeap.top().first);
}
for (int num : ans) {
cout << num << " ";
}
cout << endl;
}
int main() {
vector<int> arr;
int num;
while (cin >> num) {
arr.push_back(num);
if (cin.get() == '\n') break;
}
int k;
cin >> k;
solve(arr, k);
optimalSolution(arr, k);
return 0;
}
Question 3 : 29 April 2024
Write a function called sum_fibonacci that takes a single integer “n’ (where 1 ≤ n ≤ 50) as input and returns the sum of the first ‘n’ terms of the Fibonacci sequence.
The Fibonacci sequence is defined as follows:
- F(0) = 0
- F(1) = 1
- F(n) = F(n – 1) + F(n – 2) for n ≥ 2
Your task is to calculate the sum of the first n Fibonacci numbers, F (0) through F(n – 1), and return this sum.
Example
Input
n = 5
Output
7
Explanation
The first 5 Fibonacci numbers are: 0, 1, 1, 2, 3. Their sumis 0 + 1 + 1 + 2 + 3 = 7.
Question 4 : 29 April 2024
Given an integer array arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arre
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: arr = [0]
Output: 1
Explanation: There is only one possible result: 0.
Example 2:
Input: arr = [1,1,2]
Output: 3
Explanation: The possible subarrays are [1], [1], [2], [1,1], [1, 2], [1, 1, 2]
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.
Example 3:
Input: arr = [1,2,4]
Output: 6
Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
TCS NQT Coding Question 03 May 2024
Question 1 : 03 May 2024
A Person has many shoes of different sizes and he wants to arrange them, Calculate the number of pairs of shoes.
Example 1:
Input:
8
7L 7R 7L 8L 6R 7R 8R 6R
Output:
3
Example 2:
Input:
5
7R 7L 8R 10R 10L
Output:
2
Question 2 : 03 May 2024
In a company there are employees and their efficiency is given in array ‘arr‘ (Can be negative) you need to find the maximum efficiency of 3 employees. Efficiency of 3 employees will be calculated by multiplying their individual efficiencies from the given array.
Example 1:
Input: 5
-3 -2 7 8 1
Output: 56
Example2:
Input: 5
3 -2 -8 4 1
Output: 64
Question 3 : 03 May 2024
The Organization has data warehouse where there will be given a three digit number.
We should be saying whether the number is divisible by 9 or riot?
Example 1 :
Input: 236
Output: Number 236 is not divisible by 9
Example 2:
Input: 162
Output: Number 162 is divisible by 9
Question 4 : 03 May 2024
We are given list of numbeis we need to return maximum difference between smallest and largest number.
Example:
Input:
arr= { – 3, – 5, 1, 6, -7, 8, 11, 3}
Output:
18
Maximum difference: is 18 with pair (-7,11)
Note: Smallest number should be before largest number.
TCS NQT Coding Question 06 May 2024
Question 1 : 06 May 2024
Problem Statement: You arr given an array and n integer that contain only 5s, 6s and 7s. Your task is to sort the given array(using swapping).
Note: we need to make a ‘Final_Array’ function.
Sample test case:
Input:
n = 9
arr = {6, 5, 5, 6, 7, 6, 5, 6, 7};
output:
5 5 5 6 6 6 6 7 7