2023 AP CSA FRQ 1: AppointmentBook - Complete Solution
Share
2023 AP CSA FRQ 1: AppointmentBook
Topic: Arrays & Time Slots
Skills: Nested loops, time slot checking
Unit: Unit 2 (Selection/Iteration) | Points: 9
Study Guide: Unit 4: Data Collections
Part (a)
Write findFreeBlock.
Try it first!
public int findFreeBlock(int period, int duration) {
int freeInARow = 0;
for (int minute = 0; minute <= 59; minute++) {
if (isMinuteFree(period, minute)) {
freeInARow++;
}
else {
freeInARow = 0;
}
if (freeInARow == duration) {
return minute - freeInARow + 1;
}
}
return -1;
}Part (b)
Write makeAppointment.
public boolean makeAppointment(int startPeriod, int endPeriod, int duration) {
for (int period = startPeriod; period <= endPeriod; period++) {
int startMinute = findFreeBlock(period, duration);
if (startMinute != -1) {
reserveBlock(period, startMinute, duration);
return true;
}
}
return false;
}Key Concepts
- Track consecutive free minutes
- Return start of free block
- Use helper methods
Common Mistakes
- Wrong start minute calculation
- Not resetting counter
Author: Tanner Crow, AP CS Teacher (11+ years)