TCS NQT 2024 Coding Questions Shift-wise with Video Solution
Q1: Problem Statement:
A device named ABC is placed on a rectangular grid with M
rows and N
columns.
The device starts at the top-left corner (
1, 1) and must reach the bottom-right corner (M, N).
The device can only move East (→) or South (↓) at any step.
However, some cells are blocked (obstacles), and the device cannot pass through them.
Your task is to calculate the total number of unique paths from (1, 1)
to (M, N)
while avoiding all blocked cells.
Input Format:
- First line: Two integers
M
andN
(number of rows and columns) - Second line: An integer
K
(number of blocked cells) - Next
K
lines: Each contains two integersr
andc
(row and column of a blocked cell)
Output Format:
- Print the total number of unique valid paths from
(1,1)
to(M,N)
Constraints:
- 1 ≤ M, N ≤ 100
- 0 ≤ K ≤ M×N
Example Input:
3 3 1 2 2
Example Output:
2
Explanation:
The possible paths from (1,1) to (3,3) are:
- (1,1) → (1,2) → (1,3) → (2,3) → (3,3)
- (1,1) → (2,1) → (3,1) → (3,2) → (3,3)
The path that goes through (2,2) is blocked.
https://www.youtube.com/embed/IfOcCTwUqqQ?si=dM1eRFSKtW1CbnzZ
Q2. Problem Statement:
You are given a list of n
items sold in a store.
Each item has the following details:
- Name (string)
- Unit price (float)
- Quantity sold (integer)
Your task is to compute the following:
- Total Sales Price — sum of (unit price × quantity) for all entries.
- Average Sales Price — (Total Sales Price) ÷ n
- Top Selling Product — item name with the highest total quantity sold (combine duplicates).
Input Format:
- First line: An integer
n
— number of items - Next
n
lines: Each contains an item in the format:name unit_price quantity
Output Format:
- First line: Total Sales Price (rounded to 2 decimal places)
- Second line: Average Sales Price (rounded to 2 decimal places)
- Third line: Name of the top selling product
Example Input:
4 Apple 1.0 10 Orange 5.0 5 Banana 4.5 8 Apple 1.0 5
Example Output:
87.5 21.88 Apple
Q3. Problem Statement:
You are given an integer N
.
Your task is to print the sum of the multiplication table of N, up to N × 10
.
Input Format:
- A single integer
N
(1 ≤ N ≤ 100)
Output Format:
- A single integer representing the sum of the first 10 multiples of
N
Example Input:
10
Example Output:
550
Explanation:
Multiplication table of 10 up to 10:
10 × 1 = 10 10 × 2 = 20 10 × 3 = 30 ... 10 × 10 = 100
Sum = 10 + 20 + 30 + … + 100 = 550
Q4. Problem Statement:
You are given an array of integers and a number K
.
Your task is to print the maximum element in every contiguous subarray of size K
.
Input Format:
- First line: A space-separated list of integers representing the array
- Second line: An integer
K
(size of the subarray)
Output Format:
- A single line containing space-separated integers — the maximum of each subarray of size
K
.
Constraints:
- 1 ≤ K ≤ length of array
- Array length ≤ 10⁵
Example Input:
1 4 7 7 6 8 3
Example Output:
7 7 7 8
Explanation:
Subarrays of size 3:
- [1, 4, 7] → 7
- [4, 7, 7] → 7
- [7, 7, 6] → 7
- [7, 6, 8] → 8
TCS Actual Coding Question | How to Take input? | 29 April
Q5. Problem Statement:
You are given an integer N
.
Your task is to calculate the sum of the first N
terms of the Fibonacci series.
The Fibonacci series is defined as:0, 1, 1, 2, 3, 5, 8, 13, ...
(i.e., F(0) = 0
, F(1) = 1
, F(n) = F(n-1) + F(n-2)
for n ≥ 2)
Input Format:
- A single integer
N
(number of terms)
Output Format:
- An integer representing the sum of the first
N
Fibonacci numbers
Constraints:
- 1 ≤ N ≤ 100
Example Input:
5
Example Output:
7
Explanation:
Fibonacci terms: 0, 1, 1, 2, 3
Sum = 0 + 1 + 1 + 2 + 3 = 7
Q6: Problem Statement:
You are given an array of integers.
Your task is to perform bitwise OR operation on all possible subarrays and return the number of distinct results obtained.
Input Format:
- A single line containing space-separated integers representing the array.
Output Format:
- An integer representing the count of distinct values obtained from bitwise OR operations on all subarrays.
Constraints:
- 1 ≤ N ≤ 100 (N is the number of elements in the array)
- 1 ≤ arr[i] < 10⁹
Example:
Input:
1 2 3
Output:
3
Explanation:
Subarrays and their Bitwise OR values:
- [1] = 1
- [2] = 2
- [3] = 3
- [1,2] = 3
- [2,3] = 3
- [1,3] = 3
- [1,2,3] = 3
Unique OR results: {1, 2, 3} → Count = 3
TCS Actual Coding Question | How to Take input? | 30 April
Q7. Problem Statement:
You are given a database of student records. Each student record contains the following details:
- Name
- Age
- Grade (A, B, C, …)
- Gender (either “Male” or “Female”)
Your Task:
- Return the names of all students whose age is greater than 20.
- Return the average grade of all Female students in terms of their ASCII values (and then return the average as an integer).
Input Format:
First line contains an integer N — number of students Next N lines contain student records in the format: Name Age Grade Gender
Output Format:
- First list: Names of students with age > 20 in a list format
- Second line: Average of grades (only of Female students), in ASCII value (rounded or floored as integer)
Example Input:
3 AAA 21 A Female BBB 24 B Male CCC 26 C Female
Example Output:
['AAA', 'BBB', 'CCC'] 66
Explanation:
- All students have age > 20, so all names are listed.
- Female students: AAA (Grade A = 65), CCC (Grade C = 67)
→ Average = (65 + 67) / 2 = 66
TCS Actual Coding Question | 03 April | Shift 01
Q8. Problem Statement:
The organization has a data warehouse that stores 3-digit numbers.
Your task is to write a program that checks whether a given 3-digit number is divisible by 9 or not.
Input Format:
- A single integer
N
(a 3-digit number)
Output Format:
- Print
"Number <N> is divisible by 9"
if the number is divisible by 9 - Otherwise, print
"Number <N> is not divisible by 9"
Constraints:
- 100 ≤ N ≤ 999
Examples:
Input:
236
Output:
Number 236 is not divisible by 9
Input:
162
Output:
Number 162 is divisible by 9
Q9. Problem Statement:
You are given a list of numbers.
Your task is to return the maximum difference between a smaller number that appears before a larger number in the list.
Note:
- The smaller number must come before the larger number in the array (i.e., maintain order).
- If no such pair exists, return 0.
Input Format:
- First line: An integer
n
representing the number of elements - Second line:
n
space-separated integers
Output Format:
- A single integer representing the maximum difference
Example Input:
7 -3 -5 1 6 -7 8 11
Example Output:
18
Explanation:
- Minimum value before 11 is -7
- 11 – (-7) = 18 → This is the maximum valid difference.
TCS Actual Coding Question | 03 April | Shift 02
Q10: Problem Statement:
A person has many shoes of different sizes and orientations (Left ‘L’ or Right ‘R’).
You are given a list of shoes represented as size followed by either ‘L’ or ‘R’.
Your task is to calculate how many valid pairs of shoes can be formed.
A valid pair is made of one left and one right shoe of the same size.
Input Format:
- First line: An integer
n
(total number of shoes) - Second line:
n
space-separated strings (e.g.,7L
,7R
, etc.)
Output Format:
- A single integer representing the number of valid shoe pairs
Constraints:
- 1 ≤ n ≤ 1000
Example 1:
Input: 8 7L 7R 7L 8L 6R 7R 8R 6R Output: 3
Explanation:
- 7L appears twice, 7R appears twice → 2 pairs
- 8L and 8R → 1 pair
- 6R appears twice, but no 6L → 0 pair
→ Total = 3 pairs
Example 2:
Input: 5 7R 7L 8R 10R 10L Output: 2
Explanation:
- 7L and 7R → 1 pair
- 10L and 10R → 1 pair
→ Total = 2 pairs
Q11. Problem Statement:
In a company, there are employees and their efficiencies are given in an array arr
.
Each element represents the efficiency of an employee and can be negative.
Your task is to select any 3 employees such that the product of their efficiencies is maximum.
Input Format:
- First line: An integer
n
— number of employees - Second line:
n
space-separated integers — the efficiency values
Output Format:
- A single integer — the maximum product of efficiencies of any 3 employees
Constraints:
- 3 ≤ n ≤ 1000
- -1000 ≤ arr[i] ≤ 1000
Example 1:
Input: 5 -3 7 8 1 0 Output: 56
Explanation:
Maximum product = 7 × 8 × 1 = 56
Example 2:
Input: 5 -3 -2 4 8 1 Output: 64
Explanation:
Maximum product = (-3) × (-2) × 8 = 48
Also try 4 × 8 × 1 = 32
But (-3 × -2 × 8) = 48
Correction: Actually (-3 × -2 × 8) = 48
Typo in the image: 64 is incorrect unless the array is different.
Key Insight:
- To get the maximum product, consider:
- The product of the 3 largest numbers
- The product of the 2 smallest (most negative) numbers and the largest number
https://www.youtube.com/embed/UPYu1OqAC3U?si=yeS3ytmB–vaeqfA