Talentd
FleetCode
Make and follow sheets.Sign in

Team Dominance by Pass Success solution and explanation

Hard5 min read72.1% acceptanceUpdated 16 Oct 2026Premium elsewhere, free here

The problem

Table: Teams

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| player_id   | int     |
| team_name   | varchar | 
+-------------+---------+
player_id is the unique key for this table.
Each row contains the unique identifier for player and the name of one of the teams participating in that match.

Table: Passes

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| pass_from   | int     |
| time_stamp  | varchar |
| pass_to     | int     |
+-------------+---------+
(pass_from, time_stamp) is the primary key for this table.
pass_from is a foreign key to player_id from Teams table.
Each row represents a pass made during a match, time_stamp represents the time in minutes (00:00-90:00) when the pass was made,
pass_to is the player_id of the player receiving the pass.

Write a solution to calculate the dominance score for each team in both halves of the match. The rules are as follows:

  • A match is divided into two halves: first half (00:00-45:00 minutes) and second half (45:01-90:00 minutes)
  • The dominance score is calculated based on successful and intercepted passes:
    • When pass_to is a player from the same team: +1 point
    • When pass_to is a player from the opposing team (interception): -1 point
  • A higher dominance score indicates better passing performance

Return the result table ordered by team_name and half_number in ascending order.

The result format is in the following example.

Example:

Input:

Teams table:

+------------+-----------+
| player_id  | team_name |
+------------+-----------+
| 1          | Arsenal   |
| 2          | Arsenal   |
| 3          | Arsenal   |
| 4          | Chelsea   |
| 5          | Chelsea   |
| 6          | Chelsea   |
+------------+-----------+

Passes table:

+-----------+------------+---------+
| pass_from | time_stamp | pass_to |
+-----------+------------+---------+
| 1         | 00:15      | 2       |
| 2         | 00:45      | 3       |
| 3         | 01:15      | 1       |
| 4         | 00:30      | 1       |
| 2         | 46:00      | 3       |
| 3         | 46:15      | 4       |
| 1         | 46:45      | 2       |
| 5         | 46:30      | 6       |
+-----------+------------+---------+

Output:

+-----------+-------------+-----------+
| team_name | half_number | dominance |
+-----------+-------------+-----------+
| Arsenal   | 1           | 3         |
| Arsenal   | 2           | 1         |
| Chelsea   | 1           | -1        |
| Chelsea   | 2           | 1         |
+-----------+-------------+-----------+

Explanation:

  • First Half (00:00-45:00):
    • Arsenal's passes:
      • 1 → 2 (00:15): Successful pass (+1)
      • 2 → 3 (00:45): Successful pass (+1)
      • 3 → 1 (01:15): Successful pass (+1)
    • Chelsea's passes:
      • 4 → 1 (00:30): Intercepted by Arsenal (-1)
  • Second Half (45:01-90:00):
    • Arsenal's passes:
      • 2 → 3 (46:00): Successful pass (+1)
      • 3 → 4 (46:15): Intercepted by Chelsea (-1)
      • 1 → 2 (46:45): Successful pass (+1)
    • Chelsea's passes:
      • 5 → 6 (46:30): Successful pass (+1)
  • The results are ordered by team_name and then half_number

Approach overview

Problem Overview: You have a table of teams and a table of match records with pass data. For each team, total its successful passes across all matches, attach the team name, and rank teams by that total.

Approach 1: Correlated Subquery per Team (O(T × M) time, O(1) extra space)

For each row in the team table, run a subquery that scans the match table and sums the successful passes where that team appears. This works because each team's total is computed independently. It is easy to write and easy to verify, but the match table gets scanned once per team. Use it only to check your output on small data.

Approach 2: Equi-Join + GROUP BY with SUM (O(T + M) time, O(T) space)

Join the team table to the match table on team_id, so each match row carries the team name. Then GROUP BY team and apply SUM to the pass counts. The database can build a hash table on the team side and probe it once per match row, so each row is touched a constant number of times. This is the approach to write in an interview. In Pandas, the same idea is merge() followed by groupby().sum().

Approach 3: Window Function Ranking on Aggregated Totals (O(T log T) time, O(T) space)

Compute the per-team totals with the join and GROUP BY from Approach 2, then apply DENSE_RANK() OVER (ORDER BY total DESC). The aggregation is still linear. The extra cost comes from sorting the T aggregated rows to assign ranks. Pick this when the problem asks for ranking, and keep the aggregation as a subquery or CTE so the window function runs over one row per team.

Recommended for interviews: Interviewers expect Approach 2 as the core idea, with Approach 3 as the ranking step. The correlated subquery shows you understand the problem, but the join with GROUP BY shows you can avoid rescanning the data. Mention database joins and sorting when you explain the ranking step, and cover hash table behavior when you justify the join cost.

Solution

Thinking

A pass is +1 inside a team and -1 across teams, aggregated by team and half. Timestamps up to 45{:}00 are the first half.

Join the pass list to the team table on both player ids so the two team names can be compared.

Group by the sender's team and the half, sum dominance, and sort by team then half.

We can use an equi-join to find the teams of both the passer and the receiver for each pass. Then, based on the timestamp, we determine whether the pass occurred in the first half or the second half. By checking if the passer and receiver belong to the same team, we calculate the advantage value for each pass. Finally, we group by team name and half number, and sum the advantage values to get the advantage value for each team in the first and second halves.

# Write your MySQL query statement below
WITH
    T AS (
        SELECT
            t1.team_name,
            IF(time_stamp <= '45:00', 1, 2) half_number,
            IF(t1.team_name = t2.team_name, 1, -1) dominance
        FROM
            Passes p
            JOIN Teams t1 ON p.pass_from = t1.player_id
            JOIN Teams t2 ON p.pass_to = t2.player_id
    )
SELECT team_name, half_number, SUM(dominance) dominance
FROM T
GROUP BY 1, 2
ORDER BY 1, 2;
import pandas as pd


def calculate_team_dominance(teams: pd.DataFrame, passes: pd.DataFrame) -> pd.DataFrame:
    passes_with_teams = passes.merge(
        teams, left_on="pass_from", right_on="player_id", suffixes=("", "_team_from")
    ).merge(
        teams,
        left_on="pass_to",
        right_on="player_id",
        suffixes=("_team_from", "_team_to"),
    )
    passes_with_teams["half_number"] = passes_with_teams["time_stamp"].apply(
        lambda x: 1 if x <= "45:00" else 2
    )
    passes_with_teams["dominance"] = passes_with_teams.apply(
        lambda row: 1 if row["team_name_team_from"] == row["team_name_team_to"] else -1,
        axis=1,
    )
    result = (
        passes_with_teams.groupby(["team_name_team_from", "half_number"])["dominance"]
        .sum()
        .reset_index()
    )
    result.columns = ["team_name", "half_number", "dominance"]
    result = result.sort_values(by=["team_name", "half_number"])
    return result
Try this approach in the editor

Complexity analysis

ApproachTimeSpaceWhen to Use
Correlated Subquery per TeamO(T × M)O(1)Small data, quick verification
Equi-Join + GROUP BY with SUMO(T + M)O(T)General case, default choice
Join + GROUP BY + DENSE_RANK()O(T log T + M)O(T)When output needs a rank position

Frequently asked questions

Is Team Dominance by Pass Success easy or hard?

It is rated Hard on FleetCode, but the core logic is a join followed by a GROUP BY with SUM. The difficulty usually comes from the ranking requirement and handling edge cases like ties. Get the join and aggregation right first, then add the ranking step.

How to solve Team Dominance by Pass Success in O(n)?

Join the team table to the match table on team_id so each match row carries its team name. Then GROUP BY the team and compute SUM of successful passes. Each match row is processed once during the join and once during aggregation, so the total work is linear in the input size.

Team Dominance by Pass Success Python or Java solution?

In Python, use pandas: merge the team and match DataFrames on team_id, then call groupby on the team name and sum the pass column. In Java or any other language, the same logic applies with a hash map keyed by team_id that accumulates pass totals. The SQL version with an equi-join and GROUP BY is the most direct answer for this problem.

What is the best approach for Team Dominance by Pass Success?

The best approach is an equi-join between the team and match tables, followed by GROUP BY on the team and SUM over successful passes. This aggregates each team in a single pass over the data. If the output needs ranks, add DENSE_RANK() over the aggregated totals.

What data structure is used in Team Dominance by Pass Success?

The solution relies on a hash-based join, where the database or Pandas builds a hash table keyed by team_id. The aggregation then accumulates sums in a hash map keyed by team. Ranking with DENSE_RANK() uses a sort over the aggregated results.

What is the time complexity of Team Dominance by Pass Success?

The optimal solution runs in O(T + M) time, where T is the number of teams and M is the number of match records. The join builds and probes a hash structure, and the GROUP BY aggregates in one linear pass. Adding a ranking step sorts the T aggregated rows, which raises the cost to O(T log T) for that step.

Is Team Dominance by Pass Success asked at Google, Amazon, or Meta?

This problem is tagged as a General database problem on FleetCode and is not labeled with company-specific tags here. Expect SQL join and GROUP BY questions like this in data-focused roles at large tech companies. Practice joins, aggregations, and window functions to cover the same skills.

Comments

Now write it yourselfClose this page and solve Team Dominance by Pass Success in the editor, without looking.
Practise Team Dominance by Pass Success