Sprint View

2025 FRQ 3

7 min read

FRQ Question Description

This question involves pairing competitors in a tournament into one-on-one matches for one round of the tournament. For example, in a chess tournament, the competitors are the individual chess players. A game of chess involving two players is a match. The winner of each match goes on to a match in the next round of the tournament. Since half of the players are eliminated in each round of the tournament, there is eventually a final round consisting of one match and two competitors. The winner of that match is considered the winner of the tournament.

Competitors, matches, and rounds of the tournament are represented by the Competitor, Match, and Round classes. You will write the constructor and one method of the Round class.

Analyze the Problem: Walkthrough

Tournament Pairing Challenge

Problem Type: ArrayList Management and Pairing

Difficulty: Medium

You have an array of names in ranking order (best to worst).

You need to:

  • Create Competitor objects with names and ranks
  • Pair competitors: best with worst, 2nd-best with 2nd-worst, etc.
  • Handle odd numbers by skipping the best-ranked competitor

Part A

Write the constructor for the Round class. The constructor should initialize competitorList to contain one Competitor object for each name in the String[] names. The order in which Competitor objects appear in competitorList should be the same as the order in which they appear in names, and the rank of each competitor is based on the competitor’s position in names. Names are listed in names in order from the bestranked competitor with rank 1 to the worst-ranked competitor with rank n, where n is the number of elements in names.

For example, assume the following code segment is executed.

String[] players = {"Alex", "Ben", "Cara"}; Round r = new Round(players);

The following shows the contents of competitorList in r after the constructor has finished executing.

Complete the Round constructor.

/** Initializes competitorList, as described in part (a) */ public Round(String[] names)

Walkthrough of Part A:

Create a Competitor object for each name in the array.

Steps:

  1. Initialize competitorList as a new ArrayList<Competitor>()
  2. Loop through the names array
  3. For each name, create a Competitor with:
    • The name from the array
    • A rank equal to index + 1 (since rank starts at 1, not 0)
  4. Add each Competitor to competitorList

Example: If index is 0, rank is 1. If index is 1, rank is 2.

Quick Check:

  • What should competitorList be initialized to?
  • How do you convert index (0, 1, 2…) to rank (1, 2, 3…)?
  • What two parameters does new Competitor() need?

Part B

Write the Round method buildMatches. This method should return a new ArrayList<Match> object by pairing competitors from competitorList according to the following rules.4

• If the number of competitors in competitorList is even, the best-ranked competitor is paired with the worst-ranked competitor, the second-best-ranked competitor is paired with the second-worst-ranked competitor, etc. • If the number of competitors in competitorList is odd, the competitor with the best rank is ignored and the remaining competitors are paired according to the rule for an even number of competitors.

Each pair of competitors is used to create a Match object that should be added to the ArrayList to return. Competitors may appear in either order in a Match object, and matches may appear in any order in the returned ArrayList.

The following example shows the contents of competitorList in a Round object r1 containing an odd number of competitors and the ArrayList of Match objects that should be returned by the call r1.buildMatches().

competitorList: 2025 frq 3 image 1

The ArrayList to be returned contains a single match between Ben and Cara, the second and third-ranked competitors: 2025 frq 3 image 2

The next example shows the contents of competitorList in a Round object r2 containing an even number of competitors and the ArrayList of Match objects that should be returned by the call r2.buildMatches(). competitorList: 2025 frq 3 image 3

The ArrayList to be returned contains two matches: one match between the first- and lastranked competitors Rei and Tim, and a second match between the second- and third-ranked competitors Sam and Vi: 2025 frq 3 image 4

Walkthrough of Part B:

Pair competitors from opposite ends of the list.

Steps:

  1. Create a new ArrayList<Match> to return
  2. Determine the starting point:
    • If competitorList has an odd size: start at index 1 (skip the first)
    • If competitorList has an even size: start at index 0
  3. Calculate how many matches to create: competitorList.size() / 2
  4. Loop to create matches:
    • Get competitor from the front: competitorList.get(i)
    • Get competitor from the back: competitorList.get(size - 1 - i)
    • Create a new Match with these two competitors
    • Add the match to your list

Key formula: When pairing, use indices i and size - 1 - i to get opposite ends.

Quick Check:

  • For 5 competitors, how many matches? (Answer: 2)
  • For 5 competitors, which index do you start at? (Answer: 1)
  • For 4 competitors at indices [0,1,2,3], what pairs together? (0↔3, 1↔2)
/**
* Creates an ArrayList of Match objects for the next round
* of the tournament, as described in part (b)
* Preconditions: competitorList contains at least one element.
* competitorList is ordered from best to worst rank.
* Postcondition: competitorList is unchanged.
*/
public ArrayList<Match> buildMatches()

Code Runner Challenge

Write the constructor and one method of the Round class

View IPYNB Source
// CODE_RUNNER: Write the constructor and one method of the Round class


import java.util.ArrayList;

/** A single competitor in the tournament */
public class Competitor
{
    private String name;
    private int rank;
    
    public Competitor(String n, int initialRank)
    { /* implementation not shown */ }
}

/** A match between two competitors */
public class Match
{
    public Match(Competitor one, Competitor two)
    { /* implementation not shown */ }
}

/** A single round of the tournament */
public class Round
{
    private ArrayList<Competitor> competitorList;
    
    /** Initializes competitorList, as described in part (a) */
    public Round(String[] names)
    { /* to be implemented in part (a) */ }
    
    /** Creates an ArrayList of Match objects, as described in part (b) */
    public ArrayList<Match> buildMatches()
    { /* to be implemented in part (b) */ }
}

// Runner: Do Not Edit

public class Main {

    public static void main(String[] args) {
        System.out.println("Tournament test starting...");
        String[] players = {"Alex", "Ben", "Cara", "Dana"};
        Round r = new Round(players);
        ArrayList<Match> matches = r.buildMatches();
        System.out.println("Matches created: " + matches.size());
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Solutions

Part A

The constructor initializes the competitorList and then loops through the entire names array.

For each name, a new Competitor object is created with:

  • The name at the current index
  • A rank of i + 1 (converting array index to rank)

Each Competitor is then added to competitorList in order.

This approach guarantees:

  • All names are converted to Competitor objects
  • Ranks are assigned correctly (1 for first, 2 for second, etc.)
  • The order of competitors matches the order of names
public Round(String[] names)
{
    competitorList = new ArrayList<Competitor>();
    for(int i = 0; i < names.length; i++)
    {
    competitor c = new Competitor(names[i], i + 1);
    competitorList.add(c);
    }
}  

Part B

The method creates matches by pairing competitors from opposite ends of the list.

The logic splits into two cases:

Even number of competitors:

  • Starts at index 0
  • Loops size/2 times
  • Pairs index i with index size - i - 1
  • Example: For size 4, pairs (0,3) and (1,2)

Odd number of competitors:

  • Starts at index 1 (skips the best-ranked competitor)
  • Loops size/2 times (automatically floors the division)
  • Pairs index i with index size - i
  • Example: For size 3, pairs (1,2) only

This approach guarantees:

  • Best is paired with worst, second-best with second-worst, etc.
  • The top competitor gets a bye when there’s an odd number
  • Exactly size/2 matches are created
public ArrayList<Match> buildMatches()
{
    ArrayList<Match> matches = new ArrayList<Match>();
    if (competitorList.size() % 2 == 0)
    {
        for(int i = 0; i < competitorList.size()/2; i++)
        {
            matches.add(new Match(competitorList.get(i),
            competitorList.get(competitorList.size()  i - 1)));
        }
    }
    else
    {
        for(int i = 1; i < competitorList.size() / 2 + 1; i++)
        {
            matches.add(new Match(competitorList.get(i),
            competitorList.get(competitorList.size() - i)));
        }
    }
    return matches;
} 

Course Timeline