
Code:
class AppointmentBook
{
private boolean isMinuteFree(int period, int minute)
{
/* implementation not shown */
return true; // default stub to allow compilation
}
private void reserveBlock(int period, int startMinute, int duration)
{ /* implementation not shown */ }
/**
* Searches for the first block of duration free minutes during period.
* Returns the first minute in the block if found, otherwise returns -1.
*/
public int findFreeBlock(int period, int duration)
{
for (int start = 0; start <= 60 - duration; start++)
{
boolean free = true;
for (int m = start; m < start + duration; m++)
{
if (!isMinuteFree(period, m))
{
free = false;
break;
}
}
if (free)
{
return start;
}
}
return -1;
}
public static void main(String[] args)
{
AppointmentBook book = new AppointmentBook();
System.out.println(book.findFreeBlock(1, 10)); // expected 0 with stub
System.out.println(book.findFreeBlock(2, 30)); // expected 0
System.out.println(book.findFreeBlock(3, 60)); // expected 0
}
}
public class Main {
public static void main(String[] args) {
AppointmentBook book = new AppointmentBook();
System.out.println(book.findFreeBlock(1, 15));
System.out.println(book.findFreeBlock(4, 20));
}
}
Approach
- Loop through every possible starting minute from
0to60 - durationso the block fits within the 60-minute period. - For each starting minute, check the next
durationminutes usingisMinuteFree(period, minute). - If any minute in the block is not free, stop checking that block and move to the next starting minute.
- If all minutes in the block are free, return the starting minute; if no block works, return
-1.

class AppointmentBook
{
private boolean isMinuteFree(int period, int minute)
{
/* implementation not shown */
return true; // default stub to allow compilation
}
private void reserveBlock(int period, int startMinute, int duration)
{ /* implementation not shown */ }
public int findFreeBlock(int period, int duration)
{ /* assume works correctly */ return -1; }
/**
* Searches periods from startPeriod to endPeriod, inclusive, for a block
* of duration free minutes. If found, reserves it and returns true;
* otherwise returns false.
*/
public boolean makeAppointment(int startPeriod, int endPeriod, int duration)
{
for (int p = startPeriod; p <= endPeriod; p++)
{
int startMinute = findFreeBlock(p, duration);
if (startMinute != -1)
{
reserveBlock(p, startMinute, duration);
return true;
}
}
return false;
}
public static void main(String[] args)
{
AppointmentBook book = new AppointmentBook();
// With the stubbed findFreeBlock (returns -1), these will print false.
System.out.println(book.makeAppointment(1, 3, 15)); // expected: false (with current stub)
System.out.println(book.makeAppointment(4, 8, 30)); // expected: false (with current stub)
}
}
public class Main {
public static void main(String[] args) {
AppointmentBook book = new AppointmentBook();
System.out.println(book.makeAppointment(1, 8, 10));
}
}
Approach
- Loop through each period from
startPeriodtoendPeriod. - For each period, call
findFreeBlock(period, duration)to see if a valid block exists. - If the method returns a start minute (not
-1), callreserveBlock(period, startMinute, duration)to reserve it. - Return
trueimmediately after reserving; if no period works, returnfalse.