2009 AP CSA FRQ 3: BatteryCharger - Solution

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.

Try First! Attempt before viewing solution.

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

✓ Use modulo (%) to wrap around array indices
✓ Standard find-minimum algorithm
✓ Nested loops for sliding window cost calculation
✓ Integer.MAX_VALUE for initial minimum

Common Mistakes

Forgetting modulo for wraparound (hour 23 to 0)
Not tracking both minimum cost and start time
Off-by-one in chargeTime loop
Not initializing minimum properly

Official Resources

Author: Tanner Crow - AP CS Teacher, 11+ years

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]