Josephus Elimination Until Half Remain
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A k up to 10^9 will kill any solution that walks the circle one step at a time. That's the trap in this Bloomberg OA, reported in January 2022. It's a Josephus variant: remove people around a circle, stop when ceil(n/2) remain, and return the removal order. n is only 10^4, so plain simulation works if the index math is right. The traps are the stepping, plus k = 0, where person 1 gets removed first. If you freeze on the modulo logic during the live assessment, StealthCoder is the safety net. It runs invisibly and hands you the working approach. Know the trick first and you probably won't need it.
The problem
People labeled 1 through n stand clockwise. The current person initially is 1. Advance k positions clockwise, not counting the current person; remove the person landed on. Resume from the next survivor. Return removals in order, stopping when ceil(n/2) people remain. Function eliminateUntilHalf(n: int, k: int) → int[] Examples Example 1 n = 6 k = 4 return = [5,4,6] Exclusive counting from 1 lands on 5; continue until three survivors remain. Constraints 1 <= n <= 10^4. 0 <= k <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a list of survivors and a current index cur starting at 0. Each round, compute idx = (cur + k) % len(list). That one modulo replaces all the walking, so k = 10^9 costs nothing. Append list[idx] to the answer, pop it, and set cur = idx, because the next survivor slides into that slot. If cur now equals the new length, wrap it to 0. Repeat floor(n/2) times, since that's how many removals get you down to ceil(n/2). Check it on n=6, k=4: index 4 gives 5, then 4, then 6. Matches. Pitfalls: looping k times, counting the current person as a step, forgetting the wrap after removing the last element, and n=1 returning an empty list. List pops are O(n), so the total is O(n^2), which is fine at 10^4. If the modulo logic slips under pressure, StealthCoder is the hedge on the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Josephus Elimination Until Half Remain cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Josephus Elimination Until Half Remain FAQ
What's the trick in the Bloomberg Josephus elimination problem?+
Reduce the step with modulo. Compute idx = (cur + k) % size instead of walking k positions. With k up to 10^9, stepping one by one times out. After removal, the current index stays put because the next survivor shifts into that slot. Wrap it if it falls off the end.
How many people do I actually remove?+
floor(n/2). You stop when ceil(n/2) people remain, so removals equal n minus ceil(n/2). For n=6 that's 3 removals, which matches the example output [5,4,6]. For n=1 you remove nobody and return an empty array.
What edge cases break a naive solution here?+
Huge k is the big one, since it needs modulo. Next is k = 0, which lands on the current person, so person 1 goes first. Then n = 1 returns empty, and the index wrapping after you remove the last element in the list. Test all four before submitting.
Is O(n^2) simulation fast enough for n = 10^4?+
Yes. Each removal from a list shifts up to n elements, and you do about n/2 removals. That's tens of millions of cheap element moves at worst, which is fine. You don't need a Fenwick tree or segment tree unless the constraints were far larger.
How do I prepare for this in 48 hours?+
Hand-trace the n=6, k=4 example until you can predict each removal. Then code the list-plus-modulo version and test k=0, n=1, and k=10^9. Josephus-style circle problems reward getting the index update right, not fancy data structures. One clean pass beats five sloppy ones.