7.4.7. Free Response - Number Cube B

The following is a free response question from 2009. It was question 1 on the exam. You can see all the free response questions from past exams at https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year.

Question 1. A statistician is studying sequences of numbers obtained by repeatedly tossing a six-sided number cube. On each side of the number cube is a single number in the range of 1 to 6, inclusive, and no number is repeated on the cube. The statistician is particularly interested in runs of numbers. A run occurs when two or more consecutive tosses of the cube produce the same value. For example, in the following sequence of cube tosses, there are runs starting at positions 1, 6, 12, and 14.

../_images/numberLine.png
public class NumberCube
{
    /**
     * @return an integer value between 1 and 6, inclusive
     */
    public int toss()
    {
        /* implementation not shown */
    }

    // There may be instance variables, constructors, and methods not shown.
}

Part b. Write the method getLongestRun that takes as its parameter an array of integer values representing a series of number cube tosses. The method returns the starting index in the array of a run of maximum size. A run is defined as the repeated occurrence of the same value in two or more consecutive positions in the array. In the example array shown above there are two runs of length 4. One starts at index 6 and one at index 14. The method may return either of those indices.

If there are no runs of any value, the method returns -1.

7.4.7.1. How to Solve

Click to reveal the algorithm for solving this problem.

7.4.7.2. Mixed Up Code

Click to reveal the Mixed Up Code for the solution to this problem.

7.4.7.3. Try and Solve Part B

FRQ Number Cube B: Write the method getLongestRun that takes as its parameter an array of integer values representing a series of number cube tosses. The method returns the starting index in the array of a run of maximum size. A run is defined as the repeated occurrence of the same value in two or more consecutive positions in the array.

You have attempted of activities on this page