TCS NQT Coding Question-3: Number of Sundays

TCS NQT Coding Question-3: Number of Sundays

In this post, we will see the solution of the Python Coding Question which is already asked in the TCS NQT Exam.
Problem Statement: Ankit is always excited about sunday. It is favourite day, when he used to play all day And goes to cycling with his group. So every time when the months starts he counts the number of sundays he will get to enjoy. Considering the month can start with any day, be it Sun, Mon…. Or so on. Count the number of Sundays Ankit will get within n number of days.

Example:

Input: 
mon-> input String denoting the start of the month.
12 -> input integer denoting the number of days from the start of the month.

Output :

1 -> number of sundays within 12 days.

Explanation:

The month start with mon(Monday). So the upcoming sunday will arrive in next 6 days. And then next Sunday in next 7 days and so on.

Now total number of days are 12. It means 6 days to first sunday and then remaining 6 days will end up not with another sunday. Total 1 sundays may fall within 12 days.

Solution:

# taking inputs
day = input()
num_of_day = int(input())

# created the week day dictionary
day_dict = {"sun":0,
            "mon":1,
            "tue":2,
            "wed":3,
            "thu":4,
            "fri":5,
            "sat":6
            }

# num of days remaining for first sunday 
initial_sun = 7 - day_dict[day[:3]]

# remaining number of days after first sunday
remain_day = num_of_day - initial_sun

# based on remaining number of days number of sundays calculated
if remain_day >= 0 :
    num_sunday = 1 + (remain_day // 7)

elif remain_day < 0 and day_dict[day[:3]] == 0 :
    num_sunday = 1

else :
    num_sunday = 0

print(num_sunday)

Output:

mon
12
1

Both time and space complexity are 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