For each Team B match, count Team A matches with at most as many goals

Quick Overview

Given goal tallies for two teams, return for each of Team B's matches how many of Team A's matches scored less than or equal to it. It tests sorting with binary search for upper-bound counts, correct handling of ties, and staying well under quadratic time for lists of up to 100,000 scores.

For each Team B match, count Team A matches with at most as many goals

Company: EPAM Systems

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given two lists of goals scored by Team A and Team B across their respective matches. For each match of Team B, count how many of Team A's matches had a score less than or equal to Team B's score in that match. Return a list of counts, one for each match of Team B. ### Function Signature ```python def count_goals(team_a: list[int], team_b: list[int]) -> list[int]: ... ``` ### Rules - The output has the same length as `team_b`, and `result[i]` is the count for `team_b[i]`, in the original order of `team_b`. - A Team A score equal to the Team B score counts. - Duplicate scores in `team_a` are counted once per match. ### Constraints - `1 <= len(team_a) <= 10^5` - `1 <= len(team_b) <= 10^5` - `0 <= team_a[i], team_b[i] <= 10^9` ### Examples ```text Input: team_a = [1, 2, 3], team_b = [2, 4] Output: [2, 3] ``` For Team B's first match (score 2), the Team A scores 1 and 2 are at most 2, so the count is 2. For the second match (score 4), all 3 Team A scores are at most 4. ```text Input: team_a = [1, 4, 2, 4], team_b = [3, 5, 1] Output: [2, 4, 1] ``` For score 3 the qualifying Team A values are 1 and 2. For score 5 all 4 values qualify, and for score 1 only the value 1 does. ```text Input: team_a = [2, 2, 2], team_b = [1, 2, 0] Output: [0, 3, 0] ``` No Team A score is at most 1 or at most 0, so those counts are 0. For score 2, all three matches tie and each one counts.

Overview: Given goal tallies for two teams, return for each of Team B's matches how many of Team A's matches scored less than or equal to it. It tests sorting with binary search for upper-bound counts, correct handling of ties, and staying well under quadratic time for lists of up to 100,000 scores.

EPAM Systems logo
EPAM Systems
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given two lists of goals scored by Team A and Team B across their respective matches. For each match of Team B, count how many of Team A's matches had a score less than or equal to Team B's score in that match.

Return a list of counts, one for each match of Team B.

Function Signature

def count_goals(team_a: list[int], team_b: list[int]) -> list[int]:
    ...

Rules

  • The output has the same length as team_b , and result[i] is the count for team_b[i] , in the original order of team_b .
  • A Team A score equal to the Team B score counts.
  • Duplicate scores in team_a are counted once per match.

Constraints

  • 1 <= len(team_a) <= 10^5
  • 1 <= len(team_b) <= 10^5
  • 0 <= team_a[i], team_b[i] <= 10^9

Examples

Input: team_a = [1, 2, 3], team_b = [2, 4]
Output: [2, 3]

For Team B's first match (score 2), the Team A scores 1 and 2 are at most 2, so the count is 2. For the second match (score 4), all 3 Team A scores are at most 4.

Input: team_a = [1, 4, 2, 4], team_b = [3, 5, 1]
Output: [2, 4, 1]

For score 3 the qualifying Team A values are 1 and 2. For score 5 all 4 values qualify, and for score 1 only the value 1 does.

Input: team_a = [2, 2, 2], team_b = [1, 2, 0]
Output: [0, 3, 0]

No Team A score is at most 1 or at most 0, so those counts are 0. For score 2, all three matches tie and each one counts.