**TCS NextStep Coding Questions With Solutions 2024 | TCS NextStep Coding Questions and Answers | TCS NextStep Coding Previous Asked Questions | TCS NextStep Coding Questions in Java | TCS NextStep Coding Questions in Python**

**TCS NextStep Coding Questions With Solutions 2024: **The coding test is medium complexity, meaning that while passing it is not impossible, it is also not very simple. If you practiced some of the code questions from the previous year’s examination, you would undoubtedly have an advantage in the upcoming TCS NextStep coding exam. Let’s look at a few test patterns, TCS NextStep MCQ Questions and Answers/ TCS NextStep MCQ Coding Questions in Python, C, C++, Java, and Python code problems and their fixes. In the below section, you can find the TCS NextStep Exam Questions and Answers/TCS NextStep **Coding Questions** & Answers 2023

Previous recruiting campaigns’ coding examinations placed a strong focus on arrays and other fundamental programming principles. One can easily pass the first round and even the ones after that if they prepare adequately. You must select a language that you are comfortable with, such as C, C++, Java, or even Python, in order to complete the coding tasks. More details like TCS NextStep Coding Questions and Answers, TCS NextStep Coding Questions & Answers in C++, TCS NextStep Coding Questions & Answers, TCS NextStep Python Coding Questions & Answers, TCS NextStep Interview Questions and Answers, TCS NextStep Cut Off, Salary, Hike, Increment, Annual Appraisal, etc. are available on this page

**TCS NextStep Coding Questions and Answers For Freshers**

**Questions: **Khaled has an array A of N elements. It is guaranteed that N is even. He wants to choose at most N/2 elements from array A. It is not necessary to choose consecutive elements. Khaled is interested in XOR of all the elements he chooses. Here, XOR denotes the bitwise XOR operation.

**For example:**

- If A=[2,4,6,8], then khaled can choose the subset [2,4,8] to achieve XOR=(2 XOR 4 XOR 8)=14.

Khaled wants to maximize the XOR of all the elements he chooses. Your task is to help khaled to find the max XOR of a subset that he can achieve by choosing at most N/2 elements?

** Input format:**

- The first line contains an integer, N, denoting the number of elements in A.
- Each line i of the N subsequent lines(where 0<=i<=N) contains an integer describing Ai.

** Constraints **

- 1<=N<=120
- 1<=A[i]<=10^6

** Sample Input 1**

2

1

2

** Sample Output 1**

2

**Explanation:**

N=2, A=[1,2] khaled can choose the subset[2]. The xor of the elements in the subset is 2. And the number of elements in the subset is 1 which is less than N/2.

**Sample Input 2
**4

1

2

4

7

**Sample Output 2**

7

**Explanation:**

N=4, A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements in the subset is 7, and the number of elements in the subset is 1 which is less than N/2.

**EXAMPLE PROGRAMS**

**C++ PROGRAM**

#include<bits/stdc++.h> using namespace std; int main () { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int M = 1 << 20; int dp[M]; char res[M]; for (int i = 1; i < M; i++) dp[i] = INT_MAX; for (int i = 0; i < n; i++) { if (arr[i] == 0) continue; for (int j = 0; j < M; j++) res[j] = 0; for (int k = 0; k < M; k++) { if (res[k] == 1) continue; if (dp[k] > dp[k ^ arr[i]]) dp[k] = dp[k ^ arr[i]] + 1; else if (dp[k ^ arr[i]] > dp[k]) dp[k ^ arr[i]] = dp[k] + 1; res[k ^ arr[i]] = 1; } } int j = M - 1, k = n >> 1; while (dp[j] > k) j--; cout << j; return 0; }

**PYTHON PROGRAM**

from itertools import combinations def fun(arr, N): sub = [] max_xor = max(arr) for i in range(1, N // 2): comb = combinations(arr, i + 1) for i in comb: sub.append(list(i)) for a in sub: z = 0 for b in a: z = z ^ b if z > max_xor: max_xor = z return max_xor N = int(input("Enter Length : ")) arr = [] for i in range(N): arr.append(int(input(f"Enter {i+1} Element : "))) if N < 3: print("Output :", max(arr)) else: print("Output :", fun(arr, N))

**Question: ** You have been given a string S of length N. The given string is a binary string which consists of only 0’s and ‘1’s. Ugliness of a string is defined as the decimal number that this binary string represents.

** Example:**

- “101” represents 5.
- “0000” represents 0.
- “01010” represents 10.

There are two types of operations that can be performed on the given string.

- Swap any two characters by paying a cost of A coins.
- Flip any character by paying a cost of B coins
- flipping a character means converting a ‘1’to a ‘0’or converting a ‘0’ to a ‘1’.

Initially, you have been given coins equal to the value defined in CASH. Your task is to minimize the ugliness of the string by performing the above mentioned operations on it. Since the answer can be very large, return the answer modulo 10^9+7.

**Note:**

- You can perform an operation only if you have enough number of coins to perform it.
- After every operation the number of coins get deducted by the cost for that operation.

**Input Format**

- The first line contains an integer, N, denoting the number of character in the string
- The next line contains a string, S, denoting the the binary string
- The next line contains an integer, CASH, denoting the total number of coins present initially
- Next will contains an integer, A, denoting the cost to swap two characters.
- Then the next line contains an integer, B, denoting the cost to flip a character.

**Constraints**

- 1 <= N <= 10^5
- 1< len(S)<= 10^5
- 1<=CASH <=10^5
- 1<=A<=10^5
- 1<=B<=10^5

**Sample Input 1 :**

4

1111

7

1

2

** Sample Output 1 :**

1

** Explanation:**

3 flips can be used to create “0001” which represents 1.

** Sample Input 2:**

6

111011

7

1

3

** Sample Output 2:**

7

** Explanation:**

First swap 0 with the most significant 1, then use flip twice first on index one and then on index two “111011”=>”0111111″=>”001111″=>”000111″ the value represented is 7.

** Sample Input 3:**

6

111011

7

3

2

** Sample Output 3:**

3

** Explanation:**

Flip the 3 most significant characters to get “000011” : the value represented by this string is 3.N

**EXAMPLE PROGRAMS**

**C++ PROGRAM**

#include<bits/stdc++.h> using namespace std; string s; int n, cash, a, b; void swapf () { int i; for (int a = 0; a < s.length (); a++) if (s[a] == '1') { i = a; break; } int j = s.length () - 1; while (j > i) { if (cash < a) break; if (s[j] == '0') { if (s[i] == '0') i++; else { swap (s[i], s[j]); cash -= a; j--; } } else j--; } } void flipf () { int i; for (int a = 0; a < s.length (); a++) if (s[a] == '1') { i = a; break; } while (cash >= b) { if (i == s.length ()) break; if (s[i] == '1') { s[i] = '0'; i++; cash -= b; } } } int main () { cin >> n >> s >> cash >> a >> b; if (a < b) { swapf (); flipf (); } else { flipf (); swapf (); } cout << stoull (s, 0, 2); }

**JAVA PROGRAM**

import java.util.*; class Main { static String str; static int cash, n, a, b; static void swapf () { char s[] = str.toCharArray (); int i = 0; for (int a = 0; a < s.length; a++) if (s[a] == '1') { i = a; break; } int j = s.length - 1; while (j > i) { if (cash < a) break; if (s[j] == '0') { if (s[i] == '0') i++; else { char temp = s[i]; s[i] = s[j]; s[j] = temp; cash -= a; j--; } } else j--; } str = new String (s); } static void flipf () { char s[] = str.toCharArray (); int i = 0; for (int a = 0; a < s.length; a++) if (s[a] == '1') { i = a; break; } while (cash >= b) { if (i == s.length) break; if (s[i] == '1') { s[i] = '0'; i++; cash -= b; } } str = new String (s); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); n = sc.nextInt (); str = sc.next (); cash = sc.nextInt (); a = sc.nextInt (); b = sc.nextInt (); if (a < b) { swapf (); flipf (); } else { flipf (); swapf (); } System.out.println (Integer.parseInt (str, 2)); } }

**Question: **Wael is well-known for how much he loves the bitwise XOR operation, while kaito is well known for how much he loves to sum numbers, so their friend Resli decided to make up a problem that would enjoy both of them. Resil wrote down an array A of length N, an integer K and he defined a new function called Xor- sum as follows

- Xor-sum(x)=(x XOR A[1])+(x XOR A[2])+(x XOR A[3])+…………..+(x XOR A[N])

Can you find the integer x in the range [0,K] with the maximum Xor-sum (x) value?

Print only the value.

**Input format**

- The first line contains integer N denoting the number of elements in A.
- The next line contains an integer, k, denoting the maximum value of x.
- Each line i of the N subsequent lines(where 0<=i<=N) contains an integer describing Ai.

**Constraints**

- 1<=N<=10^5
- 0<=K<=10^9
- 0<=A[i]<=10^9

**Sample Input 1**

1

0

989898

**Sample Output 1**

989898

**Explanation:**

Xor_sum(0)=(0^989898)=989898

**Sample Input 2**

3

7

1

6

3

**Sample Output 2**

14

**Explanation**

Xor_sum(4)=(4^1)+(4^6)+(4^3)=14.

**Sample Input 3**

4

9

7

4

0

3

**Sample Output 3**

46

**Explanation:**

Xor_sum(8)=(8^7)+(8^4) +(8^0)+(8^3)=46.

**EXAMPLE PROGRAMS**

**C++ PROGRAM**

#include<bits/stdc++.h> using namespace std; unordered_map < int, int >L; int main () { int n, k, m; cin >> n >> k; vector < int >v (n); for (int i = 0; i < n; i++) { cin >> m; v[i] = m; int j = 0; while (m) { L[j] += (m & 1); m >>= 1; j++; } } int j = 0, K = k, ans = 0, ans2 = 0; while (K) { j++; K >>= 1; } for (int i = j; i > 0; i--) { if (L[i - 1] < n - L[i - 1]) ans != 1; ans <<= 1; } ans >>= 1; while (ans > k) { ans &= 0; ans <<= 1; k <<= 1; } for (auto i:v) ans2 += ans ^ i; cout << ans2; }

**JAVA PROGRAM**

import java.util.*; class Main { public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int k = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt (); int res = 0, max = Integer.MIN_VALUE; for (int i = 0; i <= k; i++) { res = 0; for (int j = 0; j < n; j++) res = res + (i ^ arr[j]); max = Math.max (res, max); } System.out.println (max); } }

**TCS NextStep Coding Questions With Solutions in Java**

**Questions: **Andy wants to go on a vacation to de-stress himself. Therefore he decides to take a trip to an island. It is given that he has as many consecutive days as possible to rest, but he can only make one trip to the island. Suppose that the days are numbered from 1 to N. Andy has M obligations in his schedule, which he has already undertaken and which correspond to some specific days. This means that ith obligation is scheduled for day Di. Andy is willing to cancel at most k of his obligations in order to take more holidays.

Your task is to find out the maximum days of vacation Andy can take by canceling at most K of his obligations.

**Input Format**

- The first line contains an integer N, denoting the total number of days
- The next line contains an integer M denoting the total number of obligations.
- The next line contains an integer K denoting the largest number of obligations he could cancel
- Each line i of the M subsequent lines (where 0<=i<=M) contains an integer describing Di.

**Constraints**

- 1<=N<=10^6
- 1<=M<=2*10^6
- 1<=K<=2*10^6
- 1<=D[i]<=10^6

**Sample Input 1:**

10

5

2

6

9

3

2

7

**Sample Output 1 :**

5

**Explanation:**

Here he could cancel his 3rd and 4th obligation which makes vacation length 5.

**Sample input 2:**

7

2

0

3

4

**Sample Output 2:**

3

**Explanation:**

Here he could not cancel any obligation since K=0, so the vacation length is 3.

**EXAMPLE PROGRAM**

**PYTHON PROGRAM**

n = int(input()) m = int(input()) k = int(input()) arr = [0] * n for i in range(m): arr[i] = int(input()) ans = 0 arr.sort() if k > 0: for i in range(k + 1, m + 3, 1): ans = max(ans, arr[i] - arr[i - k - 1] - 1) else: j = 0 while arr[j] == 0: j = j + 1 count = 0 for i in range(1, n + 1, 1): count += 1 if j < n and (i == arr[j]): count = 0 j += 1 ans = max(count, ans) print(ans)

**JAVA PROGRAM**

import java.util.*; class Main { public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int m = sc.nextInt (); int k = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < m; i++) arr[i] = sc.nextInt (); int ans = 0; Arrays.sort (arr); if (k > 0) { for (int i = k + 1; i <= m + 2; i++) ans = Math.max (ans, arr[i] - arr[i - k - 1] - 1); } else { int j = 0; while (arr[j] == 0) j++; int count = 0; for (int i = 1; i <= n; i++) { count++; if (j < n && (i == arr[j])) { count = 0; j++; } ans = Math.max (count, ans); } } System.out.println (ans); } }

**Questions: **You have been given a string S of length N. The given string is a binary string which consists of only 0’s and ‘1’s. Ugliness of a string is defined as the decimal number that this binary string represents.

** Example:**

- “101” represents 5.
- “0000” represents 0.
- “01010” represents 10.

There are two types of operations that can be performed on the given string.

- Swap any two characters by paying a cost of A coins.
- Flip any character by paying a cost of B coins
- flipping a character means converting a ‘1’to a ‘0’or converting a ‘0’ to a ‘1’.

Initially, you have been given coins equal to the value defined in CASH. Your task is to minimize the ugliness of the string by performing the above mentioned operations on it. Since the answer can be very large, return the answer modulo 10^9+7.

**Note:**

- You can perform an operation only if you have enough number of coins to perform it.
- After every operation the number of coins get deducted by the cost for that operation.

**Input Format**

- The first line contains an integer, N, denoting the number of character in the string
- The next line contains a string, S, denoting the the binary string
- The next line contains an integer, CASH, denoting the total number of coins present initially
- Next will contains an integer, A, denoting the cost to swap two characters.
- Then the next line contains an integer, B, denoting the cost to flip a character.

**Constraints**

- 1 <= N <= 10^5
- 1< len(S)<= 10^5
- 1<=CASH <=10^5
- 1<=A<=10^5
- 1<=B<=10^5

**Sample Input 1 :**

4

1111

7

1

2

** Sample Output 1 :**

1

** Explanation:**

3 flips can be used to create “0001” which represents 1.

** Sample Input 2:**

6

111011

7

1

3

** Sample Output 2:**

7

** Explanation:**

First swap 0 with the most significant 1, then use flip twice first on index one and then on index two “111011”=>”0111111″=>”001111″=>”000111″ the value represented is 7.

** Sample Input 3:**

6

111011

7

3

2

** Sample Output 3:**

3

** Explanation:**

Flip the 3 most significant characters to get “000011” : the value represented by this string is 3.N

**Example Programs:**

**JAVA PROGRAM**

import java.util.*; class Main { static String str; static int cash, n, a, b; static void swapf () { char s[] = str.toCharArray (); int i = 0; for (int a = 0; a < s.length; a++) if (s[a] == '1') { i = a; break; } int j = s.length - 1; while (j > i) { if (cash < a) break; if (s[j] == '0') { if (s[i] == '0') i++; else { char temp = s[i]; s[i] = s[j]; s[j] = temp; cash -= a; j--; } } else j--; } str = new String (s); } static void flipf () { char s[] = str.toCharArray (); int i = 0; for (int a = 0; a < s.length; a++) if (s[a] == '1') { i = a; break; } while (cash >= b) { if (i == s.length) break; if (s[i] == '1') { s[i] = '0'; i++; cash -= b; } } str = new String (s); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); n = sc.nextInt (); str = sc.next (); cash = sc.nextInt (); a = sc.nextInt (); b = sc.nextInt (); if (a < b) { swapf (); flipf (); } else { flipf (); swapf (); } System.out.println (Integer.parseInt (str, 2)); } }

**TCS NextStep Coding Questions With Solutions in Python**

**Questions: **One of the first lessons IT students learn is the representation of natural numbers in the binary number system (base 2) This system uses only two digits, 0 and 1. In everyday life we use for convenience the decimal system (base 10) which uses ten digits, from 0 to 9. In general, we could use any numbering system.

Computer scientists often use systems based on 8 or 16. The numbering system based on K uses K digits with a value from 0 to K-1. Suppose a natural number M is given, written in the decimal system To convert it to the corresponding writing in the system based on K, we successively divide M by K until we reach a quotient that is less than K

The representation of M in the system based on K is formed by the final quotient (as first digit) and is followed by the remainder of the previous divisions**For example :**

- If M=122 and K=8, 122 in base 10= 172 in base 8 This means that the number
- In decimal system = 172 in octal system.
- 172 in base 8 = 1*8^2 + 7*8 + 2 = 122

You made the following observation in applying the above rule of converting natural numbers to another numbering system

- In some cases in the new representation all the digits of the number are the same. For example 63 in base 10= 333 in base 4

Given a number M in its decimal representation, your task is find the minimum base B such that in the representation of M at base B all digits are the same.

**Input Format**

- The first line contains an integer, M, denoting the number given

**Constraints**

- 1 <= M = 10^12

**Sample Input 1 :**

41

**Sample Output 1 :**

40

**Explanation :**

Here 41 in base 40. will be 11 so it has all digits the same, and there is no smaller base satisfying the requirements

**Sample Input 2 :**

34430

**Sample Output 2 :**

312

**Explanation :**

Here 34430 in base 312 will have all digits the same and there is no smaller base satisfying the requirements

**Example Programs:**

**JAVA PROGRAM**

import java.util.*; class Main { public static boolean convertBase (int m, int base) { int rem = m % base; m = m / base; while (m >= base && (m % base == rem)) m = m / base; if (m == rem) return true; return false; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int m = sc.nextInt (); int base = 2; while (convertBase (m, base) != true) base++; System.out.println (base); } }

**C++ PROGRAM**

#include<bits/stdc++.h> using namespace std; bool converted (int M, int base) { int rem = M % base; M /= base; while (M >= base) { if (M % base != rem) return 0; M /= base; } if (M == rem) return 1; return 0; } int main () { int M; cin >> M; int base = 2; while (converted (M, base) != 1) { base++; } cout << base; return 0; }

**TCS NextStep Coding Questions With Solutions in C**

**Questions:** While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are **n **monsters you’ll need to defeat in this quest. Each monster **i **is described with two integer numbers – **poweri **and **bonusi**. To defeat this monster, you’ll need at least **poweri **experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain **bonusi **experience points if you defeat this monster. You can defeat monsters in any order.

The quest turned out to be very hard – you try to defeat the monsters but keep losing repeatedly. Your friend told you that this quest is impossible to complete. Knowing that, you’re interested, what is the maximum possible number of monsters you can defeat?

**Input:**

- The first line contains an integer, n, denoting the number of monsters. The next line contains an integer, e, denoting your initial experience.
- Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, poweri, which represents power of the corresponding monster.
- Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, bonusi, which represents bonus for defeating the corresponding monster.

**Output**

- 2

**Example Programs:**

**JAVA PROGRAM**

import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int exp = s.nextInt(); int monst[] = new int[n]; int bonus[] = new int[n]; for (int i = 0; i < n; i++) { monst[i] = s.nextInt(); } for (int i = 0; i < n; i++) { bonus[i] = s.nextInt(); } class Monster { private final int power, bonus; public Monster(int power, int bonus){ this.power = power; this.bonus = bonus; } } Monster[] monsters = new Monster[n]; for(int i = 0; i < n; i++) monsters[i] = new Monster(monst[i], bonus[i]); Arrays.sort(monsters, Comparator.comparingInt(m -> m.power)); int count = 0; for(Monster m: monsters){ if(exp < m.power) break; exp += m.bonus; ++count; } System.out.println(count); } }

JOB ALERT ON INSTAGRAM |
FOLLOW NOW>> |

JOB ALERT ON YOUR EMAIL DAILY |
SUBSCRIBE NOW>> |

Keep an eye on **Dailyrecruitment.in** for the most up-to-date information about upcoming exams and results. Find out about official notices, Exam Patterns, Syllabus, Previous Question Papers, Expected Cut Off, Results, Merit List, Study Materials, and much more. Now is the time to bookmark in order to take advantage of even more amazing career opportunities.

**Govt Jobs by Qualifications**

Education & Vacancies | Salary | Apply Link |
---|---|---|

12th Pass Govt Jobs - 18,000+ Vacancies | Rs. 5,200 - 92,300 | Apply Now |

ITI Pass Jobs - 3,500 Vacancies | Rs. 5,200 - 35,000 | Apply Now |

Any Graduate Jobs - 19,100 Vacancies | Rs. 5,200 - 92,300 | Apply Now |

Central Govt Jobs | Rs. 5,200 - 17,000 | Apply Now |

Bank Jobs - 1,000 Vacancies | Rs. 5,200 - 29,200 | Apply Now |

Diploma Jobs - 9,300 Vacancies | Rs. 5,200 - 35,000 | Apply Now |

BTech/BE Jobs - 18,000 Vacancies | Rs. 15,000 - 1,00,000 | Apply Now |

Data Entry Jobs - 1,300 Vacancies | Rs. 5,200 - 29,200 | Apply Now |

Private Jobs | Rs. 10,000 - 67,700 | Apply Now |