Skip to content

ORIGINAL CODING QUESTION

Last participant in the icebreaker circle

Participants numbered 1 to n stand in a circle for an icebreaker. Counting starts at participant 1 and moves around the circle; the participant who is counted k-th leaves, and counting restarts at 1 with the next remaining participant. Return the number of the last remaining participant.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

Two integers n k. Print the survivor number.

Constraints

1 ≤ n ≤ 200; 1 ≤ k ≤ 200. k may exceed the number of remaining participants; counting simply wraps around.

Example 1

Input

5 2

Output

3

Participants leave in the order 2, 4, 1, 5, leaving 3.

Example 2

Input

6 1

Output

6

Each counted participant leaves immediately, so 6 is last.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

A queue models the circle: the front is the next participant to be counted.

Hint 2

Move k - 1 participants from the front to the back, then remove the front.

Hint 3

Repeat until one participant remains; a recurrence on the circle size gives the same answer without simulation.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →