Fundamentals 6 min read

How to Find the Runner‑Up in a Tournament Using a Binary Tree – Java Solution

Beyond the hype of counting code lines, this article explores why quality matters and presents a classic interview problem—finding the second‑strongest contestant in a knockout tournament—using a binary‑tree simulation with a concise Java implementation that runs in optimal time.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
How to Find the Runner‑Up in a Tournament Using a Binary Tree – Java Solution

Recently a post claimed a front‑end developer was fired for writing fewer than 2,000 lines of code per day, highlighting the absurdity of using line count as a performance metric.

Quality and maintainability matter far more than raw line numbers, and evaluating developers by such metrics reflects lazy management.

Interview Question: Tournament Winner

The problem asks for the runner‑up (second strongest) contestant in a knockout tournament where each round pairs contestants and the winner advances.

The key insight is that the second strongest must be one of the players directly defeated by the champion during its path to the top, which consists of ⌊log₂ n⌋ opponents.

Solution steps:

Simulate the tournament using a complete binary tree.

Record for each match which players each winner defeats.

After the tournament, examine the champion’s defeated opponents and select the maximum among them as the runner‑up.

Below is a concise Java implementation of this approach:

import java.util.*;

public class TournamentWinner {
    static class MatchNode {
        int val;
        List<Integer> losers = new ArrayList<>();
        MatchNode(int val) { this.val = val; }
    }

    public static int findSecondLargest(int[] players) {
        Queue<MatchNode> queue = new LinkedList<>();
        for (int num : players) {
            queue.offer(new MatchNode(num));
        }
        while (queue.size() > 1) {
            MatchNode p1 = queue.poll();
            MatchNode p2 = queue.poll();
            MatchNode winner, loser;
            if (p1.val > p2.val) {
                winner = p1;
                loser = p2;
            } else {
                winner = p2;
                loser = p1;
            }
            winner.losers.add(loser.val);
            winner.losers.addAll(loser.losers);
            queue.offer(winner);
        }
        MatchNode champ = queue.poll();
        return Collections.max(champ.losers);
    }

    public static void main(String[] args) {
        int[] players = {3, 2, 8, 7, 1, 6, 5, 4};
        System.out.println("Second strongest is: " + findSecondLargest(players));
    }
}

Advantages of this solution include near‑optimal time complexity (only a small overhead beyond finding the maximum), clear logic that mirrors a real tournament, and interview‑friendly presentation that demonstrates both algorithmic insight and coding skill.

In practice, the same reasoning can be applied to identify a “successor” in many hierarchical selection processes.

JavaalgorithmInterviewData Structuresbinary treesecond largest
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.