
TCS Free NQT Exam 2-Shift Coding Question | TCS NQT Exam 21/03/2026 Coding Question | TCS Exam Question
TCS Free NQT Exam 2-Shift Coding Question | TCS NQT Exam 21/03/2026 Coding Question | TCS Exam Question
Q1. Parking Fine Calculation
Problem Statement
A parking system calculates fines based on the number of hours a vehicle is parked.
Given an integer hours representing parking duration:
- If parking time ≤ 2 hours, fine = 100
- If parking time > 2 and ≤ 5 hours, fine = 50
- If parking time > 5 hours, fine = 20
Task
Write a program to calculate and print the parking fine.
Input
An integer hours
Output
An integer representing the fine
Test Cases
Input: 2
Output: 100
Input: 4
Output: 50
Input: 6
Output: 20
C++ Solution
#include <iostream>
using namespace std;
int main() {
int hours;
cin >> hours;
if (hours <= 2)
cout << 100;
else if (hours <= 5)
cout << 50;
else
cout << 20;
return 0;
}
Java Solution
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int hours = sc.nextInt();
if (hours <= 2)
System.out.println(100);
else if (hours <= 5)
System.out.println(50);
else
System.out.println(20);
}
}
Python Solution
hours = int(input())
if hours <= 2:
print(100)
elif hours <= 5:
print(50)
else:
print(20)
TCS Free NQT Exam 2-Shift Coding Question
Q2. Maximum Sum of Elements Less Than or Equal to Given Limit
Problem Statement
You are given:
- An array of integers of size n
- An integer maxSum (maximum allowed sum)
Find the maximum possible sum of elements from the array such that the sum is less than or equal to maxSum.
You can choose any subset of elements.
Task
Return the maximum sum ≤ maxSum.
Input
- Integer n (size of array)
- Array arr[n]
- Integer maxSum
Output Format
Maximum possible sum ≤ maxSum
Test Cases
Test Case 1
Input:
n = 4
arr = [2, 3, 5, 7]
maxSum = 10
Output:
10
Explanation: subset {3, 7} gives sum 10.
Test Case 2
Input:
n = 3
arr = [4, 8, 6]
maxSum = 9
Output:
8
Explanation: best subset is {8}.
Approach (Brute Force)
- Generate all subsets
- Compute sum for each subset
- Track maximum sum ≤ maxSum
C++ Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, maxSum;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; i++)
cin >> arr[i];
cin >> maxSum;
int result = 0;
for(int i = 0; i < (1 << n); i++) {
int sum = 0;
for(int j = 0; j < n; j++) {
if(i & (1 << j))
sum += arr[j];
}
if(sum <= maxSum)
result = max(result, sum);
}
cout << result;
}
Java Solution
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int maxSum = sc.nextInt();
int result = 0;
for(int i = 0; i < (1 << n); i++) {
int sum = 0;
for(int j = 0; j < n; j++) {
if((i & (1 << j)) != 0)
sum += arr[j];
}
if(sum <= maxSum)
result = Math.max(result, sum);
}
System.out.println(result);
}
}
Python Solution
n = int(input())
arr = list(map(int, input().split()))
maxSum = int(input())
result = 0
for i in range(1 << n):
s = 0
for j in range(n):
if i & (1 << j):
s += arr[j]
if s <= maxSum:
result = max(result, s)
print(result)
Approach ( 2 Solution)
Idea Recursive
At each index, you have two choices:
- Include the current element
- Exclude the current element
Explore all possible subsets and track the maximum sum that is less than or equal to the given limit.
C++ Solution (Recursive)
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int>& arr, int index, int currentSum, int maxSum) {
// If sum exceeds limit, ignore
if(currentSum > maxSum)
return 0;
// If all elements processed
if(index == arr.size())
return currentSum;
// Include current element
int include = solve(arr, index + 1, currentSum + arr[index], maxSum);
// Exclude current element
int exclude = solve(arr, index + 1, currentSum, maxSum);
return max(include, exclude);
}
int main() {
int n, maxSum;
cin >> n;
vector<int> arr(n);
for(int i = 0; i < n; i++)
cin >> arr[i];
cin >> maxSum;
cout << solve(arr, 0, 0, maxSum);
}
Java Solution (Recursive)
import java.util.*;
public class Main {
static int solve(int[] arr, int index, int currentSum, int maxSum) {
if(currentSum > maxSum)
return 0;
if(index == arr.length)
return currentSum;
int include = solve(arr, index + 1, currentSum + arr[index], maxSum);
int exclude = solve(arr, index + 1, currentSum, maxSum);
return Math.max(include, exclude);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int maxSum = sc.nextInt();
System.out.println(solve(arr, 0, 0, maxSum));
}
}
Python Solution (Recursive)
def solve(arr, index, current_sum, max_sum):
if current_sum > max_sum:
return 0
if index == len(arr):
return current_sum
include = solve(arr, index + 1, current_sum + arr[index], max_sum)
exclude = solve(arr, index + 1, current_sum, max_sum)
return max(include, exclude)
n = int(input())
arr = list(map(int, input().split()))
max_sum = int(input())
print(solve(arr, 0, 0, max_sum))
Join our Telegram group: Click Here
Follow us on Instagram: Click Here
Join our WhatsApp group: Click Here
More Latest Off-Campus Hiring 2025 Jobs:
- Accenture New Mass Hiring 2025 – Click Here
- Naukri Campus Hiring Drive Registration –Click Here