Find the last friend remaining when every k-th friend leaves a circle

Quick Overview

Friends numbered 1 to n sit in a circle, and starting from friend 1, the k-th friend counted clockwise leaves each round until one remains. Return the label of the winner. It tests careful circular simulation, off-by-one counting, and deriving a recurrence that shrinks the game by one friend at a time.

Find the last friend remaining when every k-th friend leaves a circle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in clockwise order. More formally, moving clockwise from friend `i` brings you to friend `i + 1` for `1 <= i < n`, and moving clockwise from friend `n` brings you to friend `1`. The rules of the game are as follows: 1. Start at friend `1`. 2. Count the next `k` friends in the clockwise direction, **including** the friend you started at. The counting wraps around the circle and may count some friends more than once. 3. The last friend you counted leaves the circle and loses the game. 4. If there is still more than one friend in the circle, go back to step 2, starting from the friend immediately clockwise of the friend who just lost, and repeat. 5. Otherwise, the last friend in the circle wins the game. Given the number of friends, `n`, and an integer `k`, return the label of the friend who wins the game. ### Function Signature ```python def find_the_winner(n: int, k: int) -> int: ``` ### Constraints - `1 <= k <= n <= 500` ### Examples ```text Input: n = 5, k = 2 Output: 3 ``` Start at 1 and count 1, 2: friend 2 leaves. Start at 3 and count 3, 4: friend 4 leaves. Start at 5 and count 5, 1: friend 1 leaves. Start at 3 and count 3, 5: friend 5 leaves, so friend 3 wins. ```text Input: n = 6, k = 5 Output: 1 ``` Counting five friends each round removes them in the order 5, 4, 6, 2, 3. For example, the second round starts at 6 and counts 6, 1, 2, 3, 4, and the last round starts at 3 with only 1 and 3 left and counts 3, 1, 3, 1, 3. Friend 1 is the only one left. ```text Input: n = 4, k = 1 Output: 4 ``` With `k = 1` each round removes the friend the count starts at, so friends 1, 2 and 3 leave in turn and friend 4 wins.

Overview: Friends numbered 1 to n sit in a circle, and starting from friend 1, the k-th friend counted clockwise leaves each round until one remains. Return the label of the winner. It tests careful circular simulation, off-by-one counting, and deriving a recurrence that shrinks the game by one friend at a time.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from friend i brings you to friend i + 1 for 1 <= i < n, and moving clockwise from friend n brings you to friend 1.

The rules of the game are as follows:

  1. Start at friend 1 .
  2. Count the next k friends in the clockwise direction, including the friend you started at. The counting wraps around the circle and may count some friends more than once.
  3. The last friend you counted leaves the circle and loses the game.
  4. If there is still more than one friend in the circle, go back to step 2, starting from the friend immediately clockwise of the friend who just lost, and repeat.
  5. Otherwise, the last friend in the circle wins the game.

Given the number of friends, n, and an integer k, return the label of the friend who wins the game.

Function Signature

def find_the_winner(n: int, k: int) -> int:

Constraints

  • 1 <= k <= n <= 500

Examples

Input: n = 5, k = 2
Output: 3

Start at 1 and count 1, 2: friend 2 leaves. Start at 3 and count 3, 4: friend 4 leaves. Start at 5 and count 5, 1: friend 1 leaves. Start at 3 and count 3, 5: friend 5 leaves, so friend 3 wins.

Input: n = 6, k = 5
Output: 1

Counting five friends each round removes them in the order 5, 4, 6, 2, 3. For example, the second round starts at 6 and counts 6, 1, 2, 3, 4, and the last round starts at 3 with only 1 and 3 left and counts 3, 1, 3, 1, 3. Friend 1 is the only one left.

Input: n = 4, k = 1
Output: 4

With k = 1 each round removes the friend the count starts at, so friends 1, 2 and 3 leave in turn and friend 4 wins.