Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1535: Find the Winner of an Array Game

In this guide, we solve Leetcode #1535 Find the Winner of an Array Game in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Given an integer array arr of distinct integers and an integer k. A game will be played between the first two elements of the array (i.e.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Array, Simulation

Intuition

The rules are explicit, so simulating the process step by step is safest.

Careful state updates prevent subtle bugs.

Approach

Translate the rules into state updates and apply them in order.

Track the final state or aggregate as required.

Steps:

  • Translate rules into state updates.
  • Iterate for each step.
  • Return the final state.

Example

Input: arr = [2,1,3,5,4,6,7], k = 2 Output: 5 Explanation: Let's see the rounds of the game: Round | arr | winner | win_count 1 | [2,1,3,5,4,6,7] | 2 | 1 2 | [2,3,5,4,6,7,1] | 3 | 1 3 | [3,5,4,6,7,1,2] | 5 | 1 4 | [5,4,6,7,1,2,3] | 5 | 2 So we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games.

Python Solution

class Solution: def getWinner(self, arr: List[int], k: int) -> int: mx = arr[0] cnt = 0 for x in arr[1:]: if mx < x: mx = x cnt = 1 else: cnt += 1 if cnt == k: break return mx

Complexity

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

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy