Tcs Coding Questions 2021 -

Input: "WWWNWWWNW" Output: "FNWFNW" (first three W→F, then single N, then next three W→F, then N, then single W).

#include <iostream> #include <string> using namespace std; int main() string s; cin >> s; string result = ""; int i = 0; while(i < s.length()) if(i+2 < s.length() && s[i]=='W' && s[i+1]=='W' && s[i+2]=='W') result += 'F'; i += 3; // skip ahead to avoid overlap else result += s[i]; i++; Tcs Coding Questions 2021

def is_prime(n): if n < 2: return False for i in range(2, int(n**0.5)+1): if n % i == 0: return False return True def digit_sum(n): return sum(int(d) for d in str(n)) Input: "WWWNWWWNW" Output: "FNWFNW" (first three W→F, then

Tests nested function calls and primality checking within constraints (n ≤ 10^6). Question 2: "Cricket Fever" – Overlapping Bowlers (String & List) Problem Statement: In a cricket match, the captain maintains a string of 'W' (wicket) and 'N' (normal ball). A bowler is said to have "fever" if he takes 3 consecutive wickets (i.e., "WWW"). Given a string, replace every such occurrence of "WWW" with "F" (fever) and print the modified string. However, if two fever patterns overlap (like "WWWW" -> contains two overlapping "WWW" starting at index 0 and 1), count it only once. A bowler is said to have "fever" if

M=13. Standard greedy: 10+3 = 2 coins. But remainder after 10 =3 (divisible by 3) → forbidden. So you must choose 5+5+3 =3 coins.

"1001" → Choose substring indices 1 to 3 ("001")? Actually original: 1 0 0 1. Pick substring positions 2-4 (0,0,1) has only one '1'. Not allowed. This was so tricky that TCS 2021 actually had a simpler version: Count the number of groups of consecutive '1's.