Microsoft Online Assessment (OA) - Day of week that is K days later

Given current day as day of the week and an integer K, the task is to find the day of the week after K days.

Example 1:

Input:

day = “Monday”

K = 3

Output: Thursday

Example 2:

Input:

day = “Tuesday”

K = 101

Output: Friday

Try it yourself

Implementation

1def day_of_week(day: str, k: int) -> str:
2    days = [
3        'Monday',
4        'Tuesday',
5        'Wednesday',
6        'Thursday',
7        'Friday',
8        'Saturday',
9        'Sunday',
10    ]
11    index = 0
12    for i in range(len(days)):
13        if days[i] == day:
14            index = i
15    return days[(index + k) % 7]
16
17if __name__ == '__main__':
18    day = input()
19    k = int(input())
20    res = day_of_week(day, k)
21    print(res)
22
1import java.util.List;
2import java.util.Scanner;
3
4class Solution {
5    public static String dayOfWeek(String day, int k) {
6        List<String> days = List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
7        int index = days.indexOf(day);
8        return days.get((index + k) % 7);
9    }
10
11    public static void main(String[] args) {
12        Scanner scanner = new Scanner(System.in);
13        String day = scanner.nextLine();
14        int k = Integer.parseInt(scanner.nextLine());
15        scanner.close();
16        String res = dayOfWeek(day, k);
17        System.out.println(res);
18    }
19}
20
1function dayOfWeek(day, k) {
2    const days = [
3        'Monday',
4        'Tuesday',
5        'Wednesday',
6        'Thursday',
7        'Friday',
8        'Saturday',
9        'Sunday',
10    ];
11    const index = days.indexOf(day);
12    return days[(index + k) % 7];
13}
14
15function* main() {
16    const day = yield;
17    const k = parseInt(yield);
18    const res = dayOfWeek(day, k);
19    console.log(res);
20}
21
22class EOFError extends Error {}
23{
24    const gen = main();
25    const next = (line) => gen.next(line).done && process.exit();
26    let buf = '';
27    next();
28    process.stdin.setEncoding('utf8');
29    process.stdin.on('data', (data) => {
30        const lines = (buf + data).split('\n');
31        buf = lines.pop();
32        lines.forEach(next);
33    });
34    process.stdin.on('end', () => {
35        buf && next(buf);
36        gen.throw(new EOFError());
37    });
38}
39

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫