KatInterview

544. Output Contest Matches

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to simulate a single-elimination tournament with n teams, ranked from 1 to n. The key rule for pairing is that in each round, the strongest available team plays against the weakest available team. We need to generate a string that represents the entire structure of the tournament's matches.

Solution

Output Contest Matches in Python

class Solution:
    def findContestMatch(self, n: int) -> str:
        # Initialize a list of strings for teams 1 to n.
        teams = [str(i) for i in range(1, n + 1)]

        # Simulate rounds until only one match remains.
        while len(teams) > 1:
            next_round = []
            
            # Pair the strongest team (at the beginning) with the weakest team (at the end).
            for i in range(len(teams) // 2):
                # Create the match string: "(strongest,weakest)"
                match = f"({teams[i]},{teams[len(teams) - 1 - i]})"
                next_round.append(match)
            
            # Update the teams for the next round.
            teams = next_round

        # The final remaining element is the answer.
        return teams[0]

Output Contest Matches in Java

class Solution {
    public String findContestMatch(int n) {
        // Initialize a list of strings for teams 1 to n.
        List<String> teams = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            teams.add(String.valueOf(i));
        }

        // Simulate rounds until only one match remains.
        while (teams.size() > 1) {
            List<String> nextRound = new ArrayList<>();
            int currentSize = teams.size();
            
            // Pair the strongest team (at the beginning) with the weakest team (at the end).
            for (int i = 0; i < currentSize / 2; i++) {
                // Create the match string: "(strongest,weakest)"
                String match = "(" + teams.get(i) + "," + teams.get(currentSize - 1 - i) + ")";
                nextRound.add(match);
            }
            
            // Update the teams for the next round.
            teams = nextRound;
        }

        // The final remaining element is the answer.
        return teams.get(0);
    }
}

Output Contest Matches in C++

class Solution {
public:
    string findContestMatch(int n) {
        // Initialize a vector of strings for teams 1 to n.
        vector<string> teams;
        for (int i = 1; i <= n; ++i) {
            teams.push_back(to_string(i));
        }

        // Simulate rounds until only one match remains.
        while (teams.size() > 1) {
            vector<string> next_round;
            int current_size = teams.size();
            
            // Pair the strongest team (at the beginning) with the weakest team (at the end).
            for (int i = 0; i < current_size / 2; ++i) {
                // Create the match string: "(strongest,weakest)"
                string match = "(" + teams[i] + "," + teams[current_size - 1 - i] + ")";
                next_round.push_back(match);
            }
            
            // Update the teams for the next round.
            teams = next_round;
        }

        // The final remaining element is the answer.
        return teams[0];
    }
};

Output Contest Matches in TypeScript

function findContestMatch(n: number): string {
    // Initialize an array of strings for teams 1 to n.
    let teams: string[] = [];
    for (let i = 1; i <= n; i++) {
        teams.push(String(i));
    }

    // Simulate rounds until only one match remains.
    while (teams.length > 1) {
        const nextRound: string[] = [];
        const currentSize = teams.length;
        
        // Pair the strongest team (at the beginning) with the weakest team (at the end).
        for (let i = 0; i < currentSize / 2; i++) {
            // Create the match string: "(strongest,weakest)"
            const match = `(${teams[i]},${teams[currentSize - 1 - i]})`;
            nextRound.push(match);
        }
        
        // Update the teams for the next round.
        teams = nextRound;
    }

    // The final remaining element is the answer.
    return teams[0];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.