Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 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:
[0,2,3,4,1].[2,3,4,1,0].[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:
[1,2,0].[1,0,2].[1,2,0].Player 1 won k = 3 games in a row, so the winner is player 1.
Constraints:
n == skills.length2 <= n <= 1051 <= k <= 1091 <= skills[i] <= 106skills are unique.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[4,2,6,3,9] 2
[2,5,4] 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3175: Find The First Player to win K Games in a Row
class Solution {
public int findWinningPlayer(int[] skills, int k) {
int n = skills.length;
k = Math.min(k, n - 1);
int i = 0, cnt = 0;
for (int j = 1; j < n; ++j) {
if (skills[i] < skills[j]) {
i = j;
cnt = 1;
} else {
++cnt;
}
if (cnt == k) {
break;
}
}
return i;
}
}
// Accepted solution for LeetCode #3175: Find The First Player to win K Games in a Row
func findWinningPlayer(skills []int, k int) int {
n := len(skills)
k = min(k, n-1)
i, cnt := 0, 0
for j := 1; j < n; j++ {
if skills[i] < skills[j] {
i = j
cnt = 1
} else {
cnt++
}
if cnt == k {
break
}
}
return i
}
# Accepted solution for LeetCode #3175: Find The First Player to win K Games in a Row
class Solution:
def findWinningPlayer(self, skills: List[int], k: int) -> int:
n = len(skills)
k = min(k, n - 1)
i = cnt = 0
for j in range(1, n):
if skills[i] < skills[j]:
i = j
cnt = 1
else:
cnt += 1
if cnt == k:
break
return i
// Accepted solution for LeetCode #3175: Find The First Player to win K Games in a Row
/**
* [3175] Find The First Player to win K Games in a Row
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn find_winning_player(skills: Vec<i32>, k: i32) -> i32 {
let n = skills.len();
let k = k as usize;
let mut pos = 0;
for i in 1..n {
if skills[i] > skills[pos] {
pos = i;
}
if pos == 0 {
if i - pos >= k {
break;
}
} else {
if i - pos >= k - 1 {
break;
}
}
}
pos as i32
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3175() {
assert_eq!(1, Solution::find_winning_player(vec![1, 6, 17], 1));
assert_eq!(2, Solution::find_winning_player(vec![4, 2, 6, 3, 9], 2));
assert_eq!(1, Solution::find_winning_player(vec![2, 5, 4], 3));
}
}
// Accepted solution for LeetCode #3175: Find The First Player to win K Games in a Row
function findWinningPlayer(skills: number[], k: number): number {
const n = skills.length;
k = Math.min(k, n - 1);
let [i, cnt] = [0, 0];
for (let j = 1; j < n; ++j) {
if (skills[i] < skills[j]) {
i = j;
cnt = 1;
} else {
++cnt;
}
if (cnt === k) {
break;
}
}
return i;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.