2009 AP CSA FRQ 3: BatteryCharger
Topic: Arrays & Minimum Finding
Skills: Array traversal, finding minimum, time calculations
Unit: Unit 4 (Data Collections) (2025-2026 AP CSA)
Points: 9 | Time: ~22 minutes
Part (a)
Write the getChargeStartTime method that finds the optimal time to start charging.
Solution
public int getChargeStartTime(int chargeTime)
{
int minCost = Integer.MAX_VALUE;
int startTime = 0;
for (int i = 0; i < 24; i++)
{
int cost = 0;
for (int j = 0; j < chargeTime; j++)
{
cost += rate[(i + j) % 24];
}
if (cost < minCost)
{
minCost = cost;
startTime = i;
}
}
return startTime;
}Part (b)
Alternative implementation using helper method.
Solution
// Using helper method getChargingCost:
public int getChargeStartTime(int chargeTime)
{
int minCost = getChargingCost(0, chargeTime);
int startTime = 0;
for (int i = 1; i < 24; i++)
{
int cost = getChargingCost(i, chargeTime);
if (cost < minCost)
{
minCost = cost;
startTime = i;
}
}
return startTime;
}Key Concepts
Common Mistakes
Official Resources
Author: Tanner Crow - AP CS Teacher, 11+ years