TCS NQT Coding Question-7: Number of Red Curtains

TCS NQT Coding Question-7: Number of Red Curtains

In this post, we will see the solution of the Python Coding Question which is already asked in the TCS NQT Exam.

Problem Statement: A manufacturing company is manufacturing a new collection of curtains. The curtains are of two colors Red(R) and black (B). The curtains color is represented as a string(string) consisting of R’s and B’s of length N.Then, they are packed (substring) into L number of curtains in each box.The box with the maximum number of ‘Red’ (R) color curtains is labeled. The task here is to find the number of ‘Red’ color curtains in the labeled box.

Note : If ‘L’ is not a multiple of N, the remaining number of curtains should be considered as a substring too.In simple words, after dividing the curtains in sets of ‘L’, any curtains left will be another set(refer example 1)

Example 1:

BBBRRRBRBR -> Value of string
3 -> Value of L
3 -> Maximum number of R’s

From the input given above. Dividing the string into group of 3 characters each 

Set 1: {B,B,B}
Set 2: {R,R,R}
Set 3: {B,R,B}
Set 4: {R} -> leftover characters also as taken as another set
Among all the sets, Set 2 has more number of R’s. The number of R’s in set 2 is 3.
Hence, the output is 3.

Example 2:
RBBBRRBBB -> Value of string
5 -> Value of L
2 -> Maximum number of R’s

From the input given above,
Dividing the string into groups of 5 characters each.
Set 1: {R,B,B,B,B}
Set 2: {R,R,B,B,B}
Among both the sets, set 2 has more number of R’s. The number of R’s in set 2 is 2.
Hence, the output is 2.


The input format for testing. The candidate has to write the code to accept two inputs separated by a new line.
First input- Accept string that contains character R and B only
Second input- Accept value for N(Positive integer number)
The output format for testing. The output should be a +ve integer number of print the message (if any) given in the problem statement. (Check the output given in Example 1 and Example 2).


# taking inputs
L = int(input())
string = input()

# initialising
max_num_red, count_red = 0, 0

# iterating through each character
for var in range(len(string)) :

    # updating the max value based on substring completion
    if var % L == 0:
        max_num_red = max(max_num_red, count_red)
        count_red = 0

    # check for red colour if true
    # then increment the value by one
    if string[var] == 'R' :
        count_red += 1

# updating the maximum value
max_num_red = max(max_num_red, count_red)




Time Complexity: O(N)
Space Complexity: O(1)

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call

If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds