Skip to main content

Find The First Player to win K Games in a Row - Solution & Explanation

MediumArraySimulation20 min readAsked at: IBM
Practice this problem

Problem Statement

A competition consists of n players numbered from 0 to n - 1.

You are given an integer array skills of size n and a positive integer k, where skills[i] is the skill level of player i. All integers in skills are unique.

All players are standing in a queue in order from player 0 to player n - 1.

The competition process is as follows:

  • The first two players in the queue play a game, and the player with the higher skill level wins.
  • After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.

The winner of the competition is the first player who wins k games in a row.

Return the initial index of the winning player.

 

Example 1:

Input: skills = [4,2,6,3,9], k = 2

Output: 2

Explanation:

Initially, the queue of players is [0,1,2,3,4]. The following process happens:

  • Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is [0,2,3,4,1].
  • Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is [2,3,4,1,0].
  • Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is [2,4,1,0,3].

Player 2 won k = 2 games in a row, so the winner is player 2.

Example 2:

Input: skills = [2,5,4], k = 3

Output: 1

Explanation:

Initially, the queue of players is [0,1,2]. The following process happens:

  • Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].
  • Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is [1,0,2].
  • Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].

Player 1 won k = 3 games in a row, so the winner is player 1.

 

Constraints:

  • n == skills.length
  • 2 <= n <= 105
  • 1 <= k <= 109
  • 1 <= skills[i] <= 106
  • All integers in skills are unique.

Approach Overview

Problem Overview: You are given an array where each value represents a player's skill. Players compete from the front of the line: the first two fight, the stronger player stays at the front, and the loser moves to the end. The first player to achieve k consecutive wins is the answer.

Approach 1: Simulate the Queue (O(n + k) time, O(n) space)

This method directly follows the rules of the game using a queue-style simulation. Compare the first two players, keep the winner at the front, and push the loser to the back of the queue. Maintain a counter tracking how many consecutive wins the current champion has achieved. If the champion wins again, increment the counter; otherwise reset it for the new winner. Continue until a player reaches k wins. This approach models the game exactly and is straightforward to reason about using arrays and simulation techniques, though it may perform many iterations when k is large.

Approach 2: Early Termination Using Maximum Skill (O(n) time, O(1) space)

The key observation: the player with the maximum skill in the array can never lose. Once this player reaches the front, they will keep winning every match afterward. Track the current champion and their consecutive wins while scanning the array from left to right. Each new player challenges the champion; if their skill is higher, they become the new champion and the win counter resets to one. Otherwise the champion wins and the counter increases. As soon as the counter reaches k, return that player's index. If the scan finishes without reaching k, the maximum-skill player will eventually dominate and become the answer. This removes the need for explicit queue rotation and keeps the algorithm linear.

Recommended for interviews: The optimized maximum-skill approach is typically expected. Simulating the queue demonstrates that you understand the mechanics of the problem, but recognizing that the global maximum will eventually win indefinitely shows stronger algorithmic insight and leads to the optimal O(n) solution.

Approach 1: Simulate the Queue

This approach simulates the competition process step by step, managing the players' order using a queue-like mechanism. At each step, the first two players in the queue compete, with the winner staying ahead and the loser moving to the queue's end. We keep a count of consecutive wins for the current player at the front of the queue. The simulation stops when a player reaches k consecutive wins or when the most skillful player can potentially win k games in a row due to player order.

The solution iterates over the list of players after initializing the current winner to the first player. It checks each subsequent player to determine if they are more skilled than the current winner. If so, the current winner changes, resetting the win count. If not, the win count increases. This process continues until the win count reaches k, or until the simulation completes.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), as each player is evaluated once in a single pass through the list.
Space Complexity: O(1), as only a few variables are utilized for indexing and counting.

Try this approach in the editor β†’

Approach 2: Early Termination Using Maximum Skill

Since the skills are unique and we know the maximum possible skill in the array, an optimization can be made. If k is extremely large and insurmountable by any other than the highest skilled player, we immediately return this as the winner. This avoids unnecessary simulation when it's evident that a player can win eventually regardless of the match queue due to their superior skill.

This solution first identifies the index of the maximum skill player. If k is greater than the number of players, this player becomes the automatic winner because they can continue to win until it reaches k. Otherwise, the regular simulation proceeds.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), requires two passes for finding max skill and simulating.
Space Complexity: O(1), constant space usage.

Try this approach in the editor β†’

Approach 3: Quick Thinking

We notice that each time the first two elements of the array are compared, regardless of the result, the next comparison will always be between the next element in the array and the current winner. Therefore, if we have looped n-1 times, the final winner must be the maximum element in the array. Otherwise, if an element has won consecutively k times, then this element is the final winner.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Similar problems:

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
Simulate the Queue

Time Complexity: O(n), as each player is evaluated once in a single pass through the list.
Space Complexity: O(1), as only a few variables are utilized for indexing and counting.

Early Termination Using Maximum Skill

Time Complexity: O(n), requires two passes for finding max skill and simulating.
Space Complexity: O(1), constant space usage.

Quick Thinkingβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simulate the QueueO(n + k)O(n)When implementing the game rules directly or verifying the process step‑by‑step.
Early Termination Using Maximum SkillO(n)O(1)Best general solution; avoids full simulation and works efficiently for large k.

Video Solution

3175. Find The First Player to win K Games in a Row | 2 Approaches | One Pass | Simulation β€’ Aryan Mittal β€’ 2,056 views views

Watch 5 more video solutions β†’

Frequently Asked Questions

Is Find The First Player to win K Games in a Row easy or hard?
This problem is generally classified as Medium difficulty. The simulation idea is simple, but identifying that the maximum skill player will eventually dominate and using that insight to achieve an O(n) solution requires careful reasoning.
Find The First Player to win K Games in a Row Python/Java solution
Python and Java implementations typically follow the linear champion-tracking approach. Maintain variables for the current winner index and consecutive wins, iterate through the array, and update the champion when a stronger player appears. The algorithm remains O(n) time and O(1) space in both languages.
How to solve Find The First Player to win K Games in a Row in O(n)?
Iterate through the skills array while keeping the current champion and their consecutive win count. If the next player's skill is higher, they become the new champion and the counter resets to one. Otherwise the champion's win count increases. Return the champion once the counter reaches k, or return the maximum-skill player if the scan finishes first.
What is the best approach for Find The First Player to win K Games in a Row?
The optimal approach tracks the current champion while scanning the array and uses the observation that the maximum skill player cannot lose. Maintain a consecutive win counter for the champion and update it after each comparison. Once the counter reaches k, return that player's index. This solution runs in O(n) time and O(1) space.
Is Find The First Player to win K Games in a Row asked at Google/Amazon/Meta?
Problems based on queue simulation and competitive comparisons appear frequently in interviews at large tech companies such as Amazon, Google, and Meta. The key skill tested is recognizing patterns in repeated comparisons and optimizing a simulation into a linear scan.
What data structure is used in Find The First Player to win K Games in a Row?
The straightforward approach uses a queue-like structure to simulate players rotating after each match. The optimized solution relies only on array traversal and variables to track the current champion and consecutive wins, eliminating the need for an explicit queue.
What is the time complexity of Find The First Player to win K Games in a Row?
The optimized solution runs in O(n) time because each player is compared at most once while scanning the array. Space complexity is O(1) since only a few variables track the current champion and win count. A direct queue simulation may take O(n + k) time and O(n) space.

Ready to solve this problem?

Practice Find The First Player to win K Games in a Row with our built-in code editor and test cases.

Practice on FleetCode