Code Room
CodingMediumcod-g398
Subject MathLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

There are n people standing in a circle numbered 1 to n. Starting the count at person 1, you count k people and eliminate the k-th; counting then resumes from the next survivor. Return the 1-indexed position of the last person remaining. Constraints: 1 <= n <= 1e6, 1 <= k <= 1e6. An O(n) solution is expected (no O(n*k) circular-list simulation).

Implement
josephus(n: int, k: int) → int
Examples
in[5,2]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.