Last Server Standing When Every Kth One Is Drained in a Ring
Last updated: November 29, 2026
Quick Overview
n servers sit in a ring numbered 1 to n. Starting from server 1, count k servers clockwise (including the starting one) and drain the k-th; continue counting from the next server. Return the number of the last server left.
0
0
0 solved
n servers sit in a ring numbered 1 to n. Starting from server 1, count k servers clockwise (including the starting one) and drain the k-th; continue counting from the next server. Return the number of the last server left.
A Google phone screen problem that starts as a queue simulation of a rolling drain and ends as a recurrence. The interviewer wants the O(nk) simulation first, then the Josephus recurrence derived from re-indexing, not recited.
What the Interviewer Expects
- Simulate with a deque rotation or list index arithmetic
- Explain why list.pop(index) is O(n) and what that costs
- Derive f(n) = (f(n-1) + k) mod n by re-indexing after each removal
- Implement the O(n) iterative recurrence
- Convert between 0-based and 1-based numbering carefully
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the full drain order instead of only the survivor?
- Can you do better than O(n) when k is small and n is huge?
- What if k changes each round, say following a list?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs: n >= 1 and k >= 1. Output the 1-based label of the final remaining server. With k = 1 servers are drained in order and the answer is n....
High-Level Design
Simulation: keep a list [1..n] and an index i. Each round i = (i + k - 1) % len(lst) and pop it; the next count starts at the same index since the list shifted left. That is O(n^2) worst case du...