Next Monday After a Date

Medium ⏱ 12 min 60% acceptance ★★★★★ 4.5
Write next_weekday(d, target) returning the next date strictly after d whose weekday() equals target (0=Monday … 6=Sunday). If d itself is a Monday and target is 0, the answer is the following Monday, seven days on. Solve with modular arithmetic, not a loop.

Examples

Example 1
Input
next_weekday(date(2026, 7, 28), 0)  # a Tuesday
Output
date(2026, 8, 3)
Explanation

The Monday after Tuesday July 28 is August 3.

Constraints

  • Strictly after — never return d.
  • O(1) arithmetic, no loops.

Topics

Date & Timeweekday arithmetic

Companies

GoogleCalendlyZoho

Hints

Hint 1

days_ahead = (target - d.weekday() + 7) % 7 — but map 0 to 7.

Hint 2

Or: (target - d.weekday() - 1) % 7 + 1.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.