Josephus Survivor with Signed Direction
Updated
Problem statement
People labeled 0 through n-1 stand clockwise in increasing order around a circle. Start at person 0, counting that person as one. If k > 0, count clockwise; if k < 0, count anticlockwise. Remove the abs(k)th remaining person.
Resume at the next remaining person in that same direction, count from one again, and repeat until one person remains. Return the original label of that survivor. The direction stays fixed throughout the game.
This is a Josephus variation that accepts a negative number: pillows pass clockwise or anticlockwise and you return the last survivor. The arrangement is circular, and k is the signed direction and count.
Function
signedJosephus(n: int, k: int) → intExamples
Example 1
n = 7k = 3return = 3The removal labels are 2, 5, 1, 6, 4, 0, leaving 3.
Example 2
n = 7k = -3return = 4Counting anticlockwise removes 5, 2, 6, 1, 3, 0, leaving 4. Restart each time at the next survivor anticlockwise.
Constraints
- For this exercise, assume
1 <= n <= 5000. -1000000000 <= k <= 1000000000andk != 0.
More JP Morgan Chase Online Assessment questions
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
- Minimum Digit Changes Between String HalvesOA · Seen Sep 2026
- Same Substring Within BudgetOA · Seen Sep 2026
- Invert Ad Visibility BitsOA · Seen Aug 2026
Similar questions at other companies
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Bold Play to Reach a GoalSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Macaron Order PriceOpenAI · OAOpenAI interview questions
- Daily Settlement for Long and Short PositionsBelvedere Trading · OA
- Last Object Exit TimeIBM · OAIBM interview questions
- Debug the Risk-Limit QuoterMillennium · OAMillennium interview questions
- Prime Factor VisitationMaven Securities · OAMaven Securities interview questions
- Proportional Momentum Investment StatisticsOptiver · OAOptiver interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)