2009 AP CSA FRQ 1: NumberCube
Topic: Arrays & Run Detection
Skills: Array creation, random number simulation, run detection algorithm
Unit: Unit 4 (Data Collections) (2025-2026 AP CSA)
Points: 9 | Time: ~22 minutes
Part (a)
Write the getCubeTosses method that fills an array with random cube tosses.
Solution
public static int[] getCubeTosses(NumberCube cube, int numTosses)
{
int[] tosses = new int[numTosses];
for (int i = 0; i < numTosses; i++)
{
tosses[i] = cube.toss();
}
return tosses;
}Part (b)
Write the getLongestRun method that finds the starting index of the longest run of consecutive equal values.
Solution
public static int getLongestRun(int[] values)
{
int maxRunStart = -1;
int maxRunLength = 0;
int currentStart = 0;
int currentLength = 1;
for (int i = 1; i < values.length; i++)
{
if (values[i] == values[i - 1])
{
currentLength++;
}
else
{
if (currentLength > 1 && currentLength > maxRunLength)
{
maxRunStart = currentStart;
maxRunLength = currentLength;
}
currentStart = i;
currentLength = 1;
}
}
// Check last run
if (currentLength > 1 && currentLength > maxRunLength)
{
maxRunStart = currentStart;
}
return maxRunStart;
}Key Concepts
Common Mistakes
Official Resources
Author: Tanner Crow - AP CS Teacher, 11+ years