Ctrl K

Ball Along the Line

Easy
There are kids children standing in a line, numbered from 0 to kids - 1 from left to right. At the start child 0 holds the ball. Every second the child with the ball hands it to a neighbor. The ball first travels to the right; whenever it reaches a child at either end (0 or kids - 1), it reverses direction. Return the number of the child holding the ball after seconds seconds.
Example 1
kids=4, seconds=5 → 1

The ball goes 0 → 1 → 2 → 3, reaches the end after 3 seconds and turns back: 3 → 2 → 1. After 5 seconds child 1 has it.

Example 2
kids=2, seconds=3 → 1

Two children just toss it back and forth: 0 → 1 → 0 → 1.

Example 3
kids=6, seconds=2 → 2
Constraints
  • 2 ≤ kids ≤ 1000
  • 1 ≤ seconds ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyFold the Digits Down to One EasyCounting Off by Threes and Fives EasyAdding Long Numbers in Strings EasyGrowing Handouts Around the Circle Easy
loading Python…
Input
kids=4, seconds=5
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 85 tests on the server, hidden ones included.