# Unit 4C - Free Response Question (FRQ) 4 - Two-dimensional Arrays

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

The fourth FRQ on the AP CSA Exam as of 2026 will be on 2D Arrays and worth 6 points. Students will be provided with a scenario and its associated class(es). Students will write one method of a given class based on provided specifications and examples. The method requires students to use, analyze,and manipulate data in an **2D array** structure.

Here are two common nested loop patterns for nested loops that you will need to use in this FRQ. In the patterns below, the outer loop iterates through the rows, and the inner loop iterates through the columns. Use enhanced for-each loops if indices are not needed. Use for-loops with row and col indices if you need to access specific elements of the 2D array or to return an index. This type of loop can loop through rows first or columns first if needed.

```java
// 2D Array Traversal with Nested Loops
for (int row = 0; row < array.length; row++)
{
    for (int col = 0; col < array[0].length; col++)
    {
        if (array[row][col] ....)
            ...
    }
}

// Nested enhanced for loops
for (int[] row : array)
{
    for (int value : row)
    {
        if (value ....)
            ...
    }
}
```

The following problem is for FRQ 4 from  the [2025-2026 AP CSA Course and Exam Description (CED)](https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf).

## FRQ 4 Schedule Description

The `Appointment` class is used to store information about a person’s scheduled appointments. A partial declaration of the `Appointment` class is shown below.

```java
public class Appointment
{
    /**
    * Returns the status of the appointment ("free", "busy", etc.)
    */
    public String getStatus()
    { /* implementation not shown */ }

    /**
    * Returns the room number of the appointment location
    */
    public int getRoomNumber()
    { /* implementation not shown */ }

    /* There may be instance variables, constructors, and methods
    that are not shown. */
}
```

The `Schedule` class maintains a two-dimensional array of appointments that represents a person’s schedule. A partial declaration of the `Schedule` class is shown below. (Note that it contains a 2D array of `Appointment` objects.)

```java
public class Schedule
{
    private Appointment[][] sched;

    /**
    * Returns the index of a column containing the fewest
    * occurrences of the status indicated by the parameter
    * target
    * Preconditions: sched is not null and no elements
    * of sched are null.
    * sched has at least one row and at
    * least one column.
    */
    public int columnWithFewest(String target)
    { /* to be implemented */}

    /* There may be instance variables, constructors, and methods
    that are not shown. */
}
```

When an element of the two-dimensional array `sched` is accessed, the first index is used to specify the row and the second index is used to specify the column.

Write the `Schedule` method `columnWithFewest`. The method should return the index of a column in `sched` that contains the fewest occurrences of the parameter target. If there are multiple columns that have the fewest number of occurrences of target, any of their column indices can be returned.

Suppose `sched` has the following contents. For each element, the first value is the appointment status and the second value is the room number.

![Schedule with 2D Array of Appointment objects](images/FRQ-schedule.png)

*Schedule with 2D Array of Appointment objects*

For these contents of sched, the expected behavior of `columnWithFewest`is as follows.

- The call `columnWithFewest("busy")` should return 1 because "busy" appears two times in column 1 and more than two times in each of the other columns.
- The call `columnWithFewest("free")` should return either 2 or 3 because "free" appears one time in column 2, one time in column 3, and more than one time in each of the other columns.
- The call `columnWithFewest("hold")` should return 4 because "hold" appears zero times in column 4 and one or more times in each of the other columns.

Complete method `columnWithFewest`.

```java
/**
 * Returns the index of a column containing the fewest
 * occurrences of the status indicated by the parameter
 * target
 * Preconditions: sched is not null and no elements
* of sched are null.
 * sched has at least one row and at
 * least one column.
 */
public int columnWithFewest(String target)
```

## The Algorithm for columnWithFewest

The following exercises will help you to develop the algorithm for the method `columnWithFewest`.

> Check your understanding: Which of the following types of algorithms do we need to use to solve the problem of finding the index of a column in the `sched` 2D array that contains the fewest occurrences of the `target` status? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ4-Arrays2D.html#schedule-algorithm

> Check your understanding: Which of the following loops is the most appropriate to use for the outer loop in the `columnWithFewest` method to return the index of a column in the `sched` 2D array that contains the fewest occurrences of the `target` status? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ4-Arrays2D.html#schedule-loop-mcq

> Check your understanding: While looping through the columns and rows of the 2D array `sched`, which of the following expressions would test to see if the status of the appointment at a given row and col is equal to the `target`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ4-Arrays2D.html#schedule-status-equality

## Solve FRQ Schedule

### Exercise 1

Write the `columnWithFewest` method for the `Schedule` class.

> Open the `01-frq4-schedule` folder and edit `Schedule.java`. Press **Run** to try it; **Run Tests** checks it (2 tests).

## Grading Rubric for FRQ 4

The following AP rubric is used to grade the `columnWithFewest` method in the `Schedule` class. Each item is worth 1 point, for a total of 6 points. Did you receive all of the points? In class, your teacher may have you grade each others’ code.

![Rubric for `columnWithFewest`](images/scheduleRubric1.png)

![Rubric for `columnWithFewest`](images/scheduleRubric2.png)

*Rubric for `columnWithFewest`*
