Players with No Losses or Exactly One Loss

Quick Overview

Players with No Losses or Exactly One Loss. Each pair in matches is [winner,loser]. Consider only players appearing in at least one match. Return two increasing lists: players who never lost, then players who lost exactly once. Each ordered match outcome is unique.

Players with No Losses or Exactly One Loss

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each row `[winner, loser]` in `matches` records one match result. Consider only players who appear in at least one row. Return two lists in this order: players with no losses, then players with exactly one loss. Each list must be in increasing player-ID order. No ordered match result is repeated. ```cpp class Solution { public: vector<vector<int>> findWinners(vector<vector<int>>& matches); }; ``` ### Examples **Example 1:** ```text matches = [[1,2],[3,2],[3,4]] Output: [[1,3],[4]] Explanation: Player 2 lost twice, player 4 once, and 1 and 3 never lost. ``` **Example 2:** ```text matches = [[2,1],[1,2]] Output: [[],[1,2]] Explanation: Each player lost exactly once. ``` ### Constraints - `1 <= matches.length <= 10^5`. - Every pair has different player IDs in `[1,10^5]`; all ordered pairs are unique. ### Clarifying Questions Unseen IDs are excluded. Reverse outcomes [a,b] and [b,a] are different matches. Both output lists require increasing order. ```hint Inspect the structure How can a winner who never appears as a loser still get an entry in the loss table? ``` ### Reasoning checkpoints - Include only observed players and count losses without discarding earlier results after a later win. - Separate zero-loss and exactly-one-loss players, excluding players with more losses. - Return both lists in the required increasing order, including an empty list when appropriate. ### Optional extensions How could you also report the number of wins for each participating player?

Overview: Players with No Losses or Exactly One Loss. Each pair in matches is [winner,loser]. Consider only players appearing in at least one match. Return two increasing lists: players who never lost, then players who lost exactly once. Each ordered match outcome is unique.

Google logo
Google
May 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each row [winner, loser] in matches records one match result. Consider only players who appear in at least one row. Return two lists in this order: players with no losses, then players with exactly one loss. Each list must be in increasing player-ID order. No ordered match result is repeated.

class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches);
};

Examples

Example 1:

matches = [[1,2],[3,2],[3,4]]
Output: [[1,3],[4]]
Explanation: Player 2 lost twice, player 4 once, and 1 and 3 never lost.

Example 2:

matches = [[2,1],[1,2]]
Output: [[],[1,2]]
Explanation: Each player lost exactly once.

Constraints

  • 1 <= matches.length <= 10^5 .
  • Every pair has different player IDs in [1,10^5] ; all ordered pairs are unique.

Clarifying Questions Guidance

Unseen IDs are excluded. Reverse outcomes [a,b] and [b,a] are different matches. Both output lists require increasing order.

Reasoning checkpoints

  • Include only observed players and count losses without discarding earlier results after a later win.
  • Separate zero-loss and exactly-one-loss players, excluding players with more losses.
  • Return both lists in the required increasing order, including an empty list when appropriate.

Optional extensions

How could you also report the number of wins for each participating player?