TCS Coding Questions 2022 Day 1 Slot 2

Coding Question 1 for 2022 (September slot)

In this article, we will discuss about the TCS Coding Question which is asked in the TCS placement test. This type of Coding Questions will help you to crack your upcoming TCS exam as well as during your interview process.
TCS Coding Interview Questions for Freshers 2018

TCS Coding Question Day 1 Slot 2 – Question 1

Problem Statement

Jack is always excited about sunday. It is favourite day, when he gets to play all day. And goes to cycling with his friends.

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 Sunday, Monday…. Or so on.

Count the number of Sunday jack will get within n number of days.

Example 1:

Input

mon-> input String denoting the start of the month.

13 -> input integer denoting the number of days from the start of the month.

Output :

2 -> number of days within 13 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 13. It means 6 days to first sunday and then remaining 7 days will end up in another sunday. Total 2 sundays may fall within 13 days.

Run
#include<stdio.h>
#include<string.h>
#define MAX_LENGTH 4
int main()
{
    char s[MAX_LENGTH];
    scanf("%s", s);

    int a, ans = 0;
    scanf("%d", &a);

    // Map weekdays to their corresponding values
    char weekdays[][MAX_LENGTH] = {
        "mon", "tue", "wed", "thu", "fri", "sat", "sun"
    };
    int values[] = {6, 5, 4, 3, 2, 1, 0};

    // Find the corresponding value for the input weekday
    int mapSize = sizeof(weekdays) / sizeof(weekdays[0]);
    int m = -1;
    for (int i = 0; i < mapSize; i++) { if (strcmp(s, weekdays[i]) == 0) { m = values[i]; break; } } if (m != -1) { // Calculate the answer if (a - m > = 1) {
            ans = 1 + (a - m) / 7;
        }
    }
    
    printf("%d", ans);

    return 0;
}
Run
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s; cin >> s;
    int a,ans=0;
    cin >> a;
    unordered_map < string,int > m;
    m["mon"]=6;m["tue"]=5;m["wed"]=4;
    m["thu"]=3;m["fri"]=2;m["sat"]=1;
    m["sun"]=0;
    if(a-m[s.substr(0,3)] >=1) ans=1+(a-m[s.substr(0,3)])/7;
    cout<< ans;
}
Run
import java.util.*;
class Main
{
public static void main(String[] args)
{
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        int n=sc.nextInt();
        String arr[]={"mon","tue,","wed","thu","fri","sat","sun"};
        int i=0;
        for(i=0;i < arr.length;i++) if(arr[i].equals(str)) break; int res=1; int rem=6-i; n=n-rem; if(n > 0)
           res+=n/7;
        System.out.println(res);
      
}
} 
Run
def main():
    s = input()
    a = int(input())
    m = {
        "mon": 6, "tue": 5, "wed": 4,
        "thu": 3, "fri": 2, "sat": 1,
        "sun": 0
    }
    ans = 0
    if a - m[s[:3]] >= 1:
        ans = 1 + (a - m[s[:3]]) // 7
    print(ans)

if __name__ == "__main__":
    main()