Modulo for Cyclic Indexing

Easy ⏱ 8 min 83% acceptance ★★★★☆ 4.3
Write a function day_after(day_index, offset) that simulates cycling through a 7-day week represented as indices 0-6 (0=Monday ... 6=Sunday). Given a starting day_index and an offset (which may be large or negative), return the resulting day index, always in the range 0-6, using the modulo operator.

Examples

Example 1
Input
day_after(5, 3)
Output
1
Explanation

(5 + 3) % 7 = 1.

Example 2
Input
day_after(0, -1)
Output
6
Explanation

Wrapping backward from Monday lands on Sunday (index 6) thanks to Python modulo semantics.

Constraints

  • day_index is between 0 and 6.
  • offset can be any integer, positive, negative, or zero.

Topics

Operatorsmodulo

Companies

GoogleFlipkart

Hints

Hint 1

Python's % always returns a non-negative result when the divisor is positive, even for negative operands — exploit that.

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