Problems › TypeScript › events
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.
slotIndex(timeMinutes: int, windowStart: int, slotMinutes: int) → int
Where you start
function slotIndex(timeMinutes: number, windowStart: number, slotMinutes: number): number {
}
Worked examples
| Call | Result |
|---|---|
slotIndex(600, 540, 30) | 2 |
slotIndex(700, 540, 30) | 5 |
slotIndex(1331, 540, 30) | -1 |
slotIndex(500, 540, 30) | -1 |
Hint
Check the bounds first, then divide.
Reference solution in TypeScript
function slotIndex(timeMinutes: number, windowStart: number, slotMinutes: number): number {
if (timeMinutes < windowStart || timeMinutes >= windowStart + slotMinutes * 8) return -1;
return Math.floor((timeMinutes - windowStart) / slotMinutes);
}