Problemfitness class waitlist

Session: Sign in to solve

Solution.txt

Fitness Class Scheduler

Problem

Design FitnessClassScheduler for a gym that publishes class sessions and enrolls members according to their plans. The scheduler must prevent time conflicts, respect capacity, and promote eligible members from each first-in, first-out waitlist. Calls are sequential.

Requirements

  • Identifiers. Every plan ID, member ID, session ID, and class type contains 1 to 64 ASCII letters, digits, _, or -.
  • Membership plans. defineMembershipPlan creates an immutable plan with 1 to 50 distinct allowed class types. A plan ID can be defined only once.
  • Members. registerMember creates an immutable member under an existing plan. A member ID can be registered only once.
  • Sessions. publishSession creates a session with a unique permanent ID, a valid class type, 0 <= startMinute < endMinute <= 1000000000, and capacity from 1 to 10000. A canceled session ID cannot be reused.
  • Enrollment. A member may enroll only when the member and active session exist, the member's plan allows the class type, the session has capacity, and the session's half-open interval [startMinute, endMinute) does not overlap any session in the member's enrolled schedule.
  • Explicit waitlisting. joinWaitlist succeeds only for a full active session when the member is eligible, has no enrolled schedule overlap with it, and has neither an enrollment nor a waitlist claim for that session. A failed enrollment never adds a waitlist claim.
  • Single claim. A member has at most one claim for a session, either enrolled or waitlisted.
  • Withdrawal. withdraw removes an enrollment. The freed seat goes to the earliest waitlisted member whose current enrolled schedule does not overlap the session. Earlier waitlist claims that now conflict are removed permanently. At most one member is promoted for one freed seat.
  • Waitlist departure. leaveWaitlist removes an existing waitlist claim without changing enrollment.
  • Cancellation. cancelSession is terminal. It removes every enrollment and waitlist claim for that session and removes the session from all member schedules.
  • Schedule query. getSchedule returns the enrolled session IDs ordered by (startMinute, endMinute, sessionId). It returns an empty array for an invalid or unknown member ID.
  • Rejected mutations. Every mutating method returns false for invalid input or a rejected operation and leaves all state unchanged. A successful mutation returns true.

API

SignatureReturnsBehavior
FitnessClassScheduler()Not applicableCreates an empty scheduler.
defineMembershipPlan(planId: string, allowedClassTypes: string[])booleanDefines one immutable plan if its ID and class-type set are valid and unused.
registerMember(memberId: string, planId: string)booleanRegisters one member under an existing plan if the member ID is valid and unused.
publishSession(sessionId: string, classType: string, startMinute: integer, endMinute: integer, capacity: integer)booleanPublishes one active session when every argument is valid and the session ID has never been used.
enroll(memberId: string, sessionId: string)booleanEnrolls an eligible member when capacity, overlap, and single-claim rules allow it.
joinWaitlist(memberId: string, sessionId: string)booleanAppends an eligible member to the FIFO waitlist of a full session.
withdraw(memberId: string, sessionId: string)booleanRemoves an enrollment and applies the promotion rule.
leaveWaitlist(memberId: string, sessionId: string)booleanRemoves the member's existing waitlist claim.
cancelSession(sessionId: string)booleanPermanently cancels an active session and clears its claims.
getSchedule(memberId: string)string[]Returns the member's ordered enrolled session IDs, or an empty array for an invalid or unknown ID.

Examples

StepOperationResult
1defineMembershipPlan("standard", ["yoga"])true
2registerMember("m1", "standard")true
3registerMember("m2", "standard")true
4publishSession("morning", "yoga", 60, 120, 1)true
5enroll("m1", "morning")true
6joinWaitlist("m2", "morning")true
7withdraw("m1", "morning")true; m2 is promoted.
8getSchedule("m2")["morning"]

Constraints

  • At most 50 plans may be defined.
  • At most 20000 members and 20000 sessions may be created.
  • At most 200000 public method calls are made.
  • Calls are sequential.

Notes

Intervals are half-open, so sessions ending and starting at the same minute do not overlap.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.