Skip to interview prep
Apexed

89. Josephus Circle Survivor

MediumQueues

People numbered 1 to n stand in a circle. Starting from person 1, count k people clockwise (including the starting person); the k-th person leaves the circle. Counting resumes from the next person. Return the number of the last person remaining.

Example 1

Input: n = 5, k = 2
Output: 3
Explanation: People leave in the order 2, 4, 1, 5.

Constraints

  • 1 ≤ n ≤ 500
  • 1 ≤ k ≤ 500

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Josephus Circle Survivor — coding interview practice — Apexed