Schedule is #4 from the from the 2025 AP Computer Science A Course Description sample problems.

2025 AP CS A Course Description

The sample free response start on PDF page 168 (labeled page 161 at the bottom right).

columnWithFewest method (with helper method)

public int columnWithFewest(String target)
{
    int colWithFewest = 0;

    for(int c = 1; c < sched[0].length; c++)
        if(countInCol(target, c) < countInCol(target, colWithFewest))
            colWithFewest = c;

    return colWithFewest;
}

private int countInCol(String target, int col)
{
    int count = 0;

    for(int r = 0; r < sched.length; r++)
        if(sched[r][col].getStatus().equals(target))
            count++;

    return count;
}

This solution uses a helper method countInCol to count the number of appointments in given column that match a given target status. The helper method makes columnWithFewest easier to write.

Although you should not seek out opportunities to write your own helper methods on the AP CS A Exam, solutions that use them correctly generally receive full credit. Exceptions include if the solution does not meet required constraints or if the helper method cannot exist within the context (ex: a helper method in a different class).

This is a variation of Finding the min or max. colWithFewest starts at 0 because that is the index of the first column that might contain the fewest appointments with the given target status.

Storing both the column index and the number of appointments in the column with the fewest would result in slightly more efficient code. The solution above favors code that is easier to read and less likely to contain an error (since it doesn’t have to ensure that two variables remain in sync).

columnWithFewest method (without helper method)

public int columnWithFewest(String target)
{
    int lowestCount = Integer.MAX_VALUE;
    int colWithFewest = -1;

    for(int c = 0; c < sched[0].length; c++)
    {
        int count = 0;

        for(int r = 0; r < sched.length; r++)
            if(sched[r][c].getStatus().equals(target))
                count++;

        if(count < lowestCount)
        {
            lowestCount = count;
            colWithFewest = c;
        }
    }

    return colWithFewest;
}

This is a variation of Finding the min or max. Storing both lowestCount and colWithFewest isn’t ideal but significantly simplifies the code.

Java files with test code

ScheduleTester.java includes JUnit 5 test code with the examples from the problem. See Running JUnit 5 tests.

Appointment.java
Schedule.java
ScheduleTester.java

2025 AP CS A Course Description Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

See an error? Question? Please comment below.

Comment on Schedule free response answer