# Solution to CandidatePool free response

Complete the CandidatePool practice problem before reviewing the solution.

Review the CandidatePool solution with AP CS Tutor Brandon Horn.

## Part (a) `getCandidatesForPosition` method

``````public ArrayList<Candidate> getCandidatesForPosition(String position)
{
ArrayList<Candidate> candidates = new ArrayList<Candidate>();

for (Candidate candidate : pool)
if (candidate.getPosition().equals(position))

return candidates;
}
``````

This problem requires students to traverse a list and check something about each element for equality with a parameter. Matching elements must be inserted into a list declared, initialized, and returned by the student. The solution above demonstrates a for each loop with which students need to be familiar.

List traversal is commonly tested as is the ability to accurately check values for equality (`==` for primitive types, `equals` for object types). Students must also be able to properly declare, initialize, and work with ArrayList objects.

## Part (b)

``````public Candidate getBestCandidate(String position)
{
ArrayList<Candidate> candidates = getCandidatesForPosition(position);

if (candidates.size() == 0)
return null;

Candidate bestCandidate = candidates.get(0);

for (int i = 1; i < candidates.size(); i++)
if (candidates.get(i).getInterviewScore() > bestCandidate.getInterviewScore())
bestCandidate = candidates.get(i);

return bestCandidate;
}
``````

Finding and returning the minimum or maximum from a list of elements is commonly tested. The standard algorithm is to check each value against the min/max so far. If the list is known to contain at least 1 element, the first element should be used as the initial value.

This problem does not specifically require students to use the method from Part (a), though doing so does make the problem easier. On the AP CS A Exam, the newer free response questions are often explicit if correct use of an existing method is required. Solutions that attempt to reimplement functionality rather than call an existing method rarely receive full credit.

This problem also requires that students handle a special case differently from the normal case. The method returns `null` if there are no matching candidates. This is a common requirement and is easily addressed by paying close attention to the problem description.

## Part (c) `removeCandidatesForPosition` method

``````public int removeCandidatesForPosition(String position)
{
int removed = 0;

for (int i = pool.size() - 1; i >= 0; i--)
{
if (pool.get(i).getPosition().equals(position))
{
pool.remove(i);
removed++;
}
}

return removed;
}
``````

Removal of elements matching some criteria is commonly tested. The solution above traverses the list backwards to prevent the shift that occurs when an element is removed from affecting the check of subsequent elements. Other techniques for addressing this issue also work. Note that a for each loop cannot be used to remove elements.

This problem requires students to track the number of elements removed. Students are often asked to keep track of something about the elements removed.