Which slot does this time fall into
A booking window is split into equal-length slots. Given a time in minutes, return the slot number or -1 if it does not belong.
- The window starts at windowStart and each slot is slotMinutes long.
- There are exactly 8 slots in the window.
- The slot index is 0-based: (timeMinutes − windowStart) / slotMinutes using integer division.
- If timeMinutes is before windowStart or at or after windowStart + slotMinutes × 8, return -1.
slot_index(time_minutes: int, window_start: int, slot_minutes: int) → int
Where you start
def slot_index(time_minutes: int, window_start: int, slot_minutes: int) -> int:
Worked examples
| Call | Result |
|---|---|
slot_index(600, 540, 30) | 2 |
slot_index(700, 540, 30) | 5 |
slot_index(1331, 540, 30) | -1 |
slot_index(500, 540, 30) | -1 |
Hint
Check the bounds first, then divide.
Reference solution in Python
def slot_index(time_minutes: int, window_start: int, slot_minutes: int) -> int:
if time_minutes < window_start or time_minutes >= window_start + slot_minutes * 8:
return -1
return (time_minutes - window_start) // slot_minutes