Problem · Math

Last Candidate Standing

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEPHONE SCREEN

Problem statement

Candidates numbered 1 through n stand in a circle. Starting at candidate 1, repeatedly count k remaining candidates, including the current candidate as count one. Eliminate the candidate counted as k, then continue from the next remaining candidate.

Return the number of the final candidate.

Function

lastCandidateStanding(n: int, k: int) → int

Examples

Example 1

n = 5k = 2return = 3

Candidates 2, 4, 1, and 5 are eliminated, leaving 3.

Example 2

n = 7k = 3return = 4

The standard Josephus recurrence leaves candidate 4.

Constraints

  • 1 <= n <= 10000000
  • 1 <= k <= 10^9

More Goldman Sachs problems

drafts saved locally
public int lastCandidateStanding(int n, int k) {
  // write your code here
}
n5
k2
expected3
checking account