# Solution to CandidatePool free response practice question

Complete the CandidatePool practice problem before reviewing the solution.

Review the CandidatePool solution with AP CS Tutor Brandon Horn.

## Part (a)

```public List<Candidate> getCandidatesForPosition(String position)
{
List<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)
{
List<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 strategy is always the same: check each value against the min/max so far. If the list is known to contain at least 1 element, the first element can be used as the initial value.

This problem requires students to use a method from a previous part. Students must call the method and assume that it works. Solutions that attempt to reimplement functionality rather than call an existing method rarely receive full credit.

The 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)

```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 from a list 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. Although it is possible to traverse the list forward, students who do so must carefully avoid incrementing the loop control variable after removing an element. 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. 