Elimination Game

MediumMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Start with the list 1, 2, ..., n. Repeat the following until one number remains: 1. Going left to right, remove the first number and then every other number after it. 2. Going right to left on what is left, remove the last number and then every other number before it. The two directions keep alternating. Return the last remaining number. n can be up to 10^9, so simulating the list is not acceptable in the intended solution.

Examples

Input: n = 9
Output: 6
[1..9] -> [2,4,6,8] -> [2,6] -> [6].
Input: n = 1
Output: 1
Only one number is present.

Constraints

  • 1 <= n <= 10^9
  • The first pass always goes left to right
  • Passes alternate direction

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems