
TCS Free NQT Exam 1-Shift Coding Question | TCS NQT Exam 21/03/2026 Coding Question | TCS Exam Question
TCS Free NQT Exam 1-Shift Coding Question | TCS NQT Exam 21/03/2026 Coding Question | TCS Exam Question
Q1. Discount Calculator
Problem Statement
You are given a purchase amount. Based on the amount, a discount is applied as follows:
Discount Rules
| Amount Range | Discount |
|---|---|
| amount < 1000 | 5% |
| 1000 ≤ amount < 5000 | 10% |
| amount ≥ 5000 | 15% |
Task:
Calculate the final payable amount after applying the discount.
Input Format
A single integer amount representing the total purchase value.
Output Format
Print the final amount after discount, rounded to 2 decimal places.
Sample Test Cases
Test Case 1
Input:
800
Output:
760.00
Explanation:
5% of 800 = 40 → Final = 800 – 40 = 760
Test Case 2
Input:
2000
Output:
1800.00
Explanation:
10% of 2000 = 200 → Final = 1800
Test Case 3
Input:
6000
Output:
5100.00
Explanation
15% of 6000 = 900 → Final = 5100
Solutions
C++ Solution
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double amount;
cin >> amount;
double discount = 0;
if (amount < 1000)
discount = 0.05;
else if (amount < 5000)
discount = 0.10;
else
discount = 0.15;
double finalAmount = amount - (amount * discount);
cout << fixed << setprecision(2) << finalAmount;
return 0;
}
Java Solution
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double amount = sc.nextDouble();
double discount;
if (amount < 1000)
discount = 0.05;
else if (amount < 5000)
discount = 0.10;
else
discount = 0.15;
double finalAmount = amount - (amount * discount);
System.out.printf("%.2f", finalAmount);
}
}
Python Solution
amount = float(input())
if amount < 1000:
discount = 0.05
elif amount < 5000:
discount = 0.10
else:
discount = 0.15
final_amount = amount - (amount * discount)
print(f"{final_amount:.2f}")
Q2. Arrange the King’s Army
Problem Statement
A king wants to arrange his army in a line following strict rules.
Given
- N → total number of soldiers in the arrangement
- R → soldiers numbered from 1 to R
- end → the last soldier in the arrangement must be this number
Rules
- The arrangement must start with soldier 1
- The arrangement must end with the soldier end
- No two adjacent soldiers can have the same number
- You can use any soldier number from 1 to R multiple times
Task
Find the total number of valid arrangements of length N that satisfy all the above conditions.
Input Format
Three integers:
N R end
Output Format
A single integer → number of valid arrangements
Sample Test Cases
Test Case 1
Input:
4 4 3
Output:
7
Test Case 2
Input:
3 3 2
Output:
2
Explanation
Valid sequences:
1 2 2 (invalid, same adjacent)
1 3 2
1 2 3 (wrong end)
1 2 1 (wrong end)
So answer = 2
Approach (Dynamic Programming)
Let:
dp[i][j] = number of ways to form a sequence of length i ending with soldier j
Transition
For each position:
dp[i][j] += dp[i-1][k] where k ≠ j
Initialization
dp[1][1] = 1
Final Answer
dp[N][end]
Brute Force Idea
We will generate all possible sequences of length N using numbers from 1 to R, and then:
Check conditions:
1. First element must be 1
2. Last element must be end
3. No two adjacent elements are equal
Approach:
- Use recursion (backtracking)
- Build sequence step by step
At each step:
Try all values from 1 to R
Skip if same as previous (to avoid adjacent duplicates)
Count valid sequences
Complexity
- Time: O(R^N) (very slow, only for small N)
- Space: O(N)
C++ Brute Force
#include <bits/stdc++.h>
using namespace std;
int N, R, END;
int countWays = 0;
void generate(vector<int> &arr, int pos) {
if (pos == N) {
if (arr[N - 1] == END) countWays++;
return;
}
for (int i = 1; i <= R; i++) {
if (pos > 0 && arr[pos - 1] == i) continue;
arr[pos] = i;
generate(arr, pos + 1);
}
}
int main() {
cin >> N >> R >> END;
vector<int> arr(N);
arr[0] = 1;
generate(arr, 1);
cout << countWays;
return 0;
}
Java Brute Force
import java.util.*;
public class Main {
static int N, R, END;
static int countWays = 0;
static void generate(int[] arr, int pos) {
if (pos == N) {
if (arr[N - 1] == END)
countWays++;
return;
}
for (int i = 1; i <= R; i++) {
// Avoid same adjacent soldiers
if (pos > 0 && arr[pos - 1] == i)
continue;
arr[pos] = i;
generate(arr, pos + 1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
R = sc.nextInt();
END = sc.nextInt();
int[] arr = new int[N];
// First soldier must be 1
arr[0] = 1;
generate(arr, 1);
System.out.println(countWays);
}
}
Python Brute Force
N, R, END = map(int, input().split())
count = 0
arr = [0] * N
def generate(pos):
global count
if pos == N:
if arr[N - 1] == END:
count += 1
return
for i in range(1, R + 1):
# Avoid same adjacent soldiers
if pos > 0 and arr[pos - 1] == i:
continue
arr[pos] = i
generate(pos + 1)
# First element must be 1
arr[0] = 1
generate(1)
print(count)
Important Notes:
- Works only for small values (e.g., N ≤ 10)
- Used to understand logic or in interviews to explain thinking
- For large constraints → use DP (optimized solution)
Solution
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, R, end;
cin >> N >> R >> end;
vector<vector<int>> dp(N + 1, vector<int>(R + 1, 0));
dp[1][1] = 1;
for (int i = 2; i <= N; i++) {
for (int j = 1; j <= R; j++) {
for (int k = 1; k <= R; k++) {
if (k != j) {
dp[i][j] += dp[i - 1][k];
}
}
}
}
cout << dp[N][end];
}
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int R = sc.nextInt();
int end = sc.nextInt();
int[][] dp = new int[N + 1][R + 1];
dp[1][1] = 1;
for (int i = 2; i <= N; i++) {
for (int j = 1; j <= R; j++) {
for (int k = 1; k <= R; k++) {
if (k != j) {
dp[i][j] += dp[i - 1][k];
}
}
}
}
System.out.println(dp[N][end]);
}
}
Python
N, R, end = map(int, input().split())
dp = [[0] * (R + 1) for _ in range(N + 1)]
dp[1][1] = 1
for i in range(2, N + 1):
for j in range(1, R + 1):
for k in range(1, R + 1):
if k != j:
dp[i][j] += dp[i - 1][k]
print(dp[N][end])
Optimization
Instead of three loops, use:
dp[i][j] = total(dp[i-1]) - dp[i-1][j]
This reduces complexity from O(N × R²) to O(N × R).
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