// Bug: strict < comparisons reject a slot that ends exactly at a busy start or at dayEnd, and busy is sorted in place.export type Interval = { start: number; end: number };export class SlotError extends Error { constructor(public readonly kind: "invalid_input") { super(kind); }}export function findFreeSlot(busy: Interval[], dayStart: number, dayEnd: number, durationMin: number): number | undefined { if (durationMin <= 0 || dayEnd <= dayStart) throw new SlotError("invalid_input"); for (const b of busy) { if (b.end <= b.start) throw new SlotError("invalid_input"); } busy.sort((a, b) => a.start - b.start); let cursor = dayStart; for (const b of busy) { if (cursor + durationMin < b.start) break; cursor = Math.max(cursor, b.end); } return cursor + durationMin < dayEnd ? cursor : undefined;}