Code RoomJosephus problem
MediumPrep Room Coding #967

Josephus problem

CodingAlgorithms & data structuresMid–Senior~25 min

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.

0:00 of about 25 min
InputExpectedGot
[5,2]3not run yetsample