Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You are given two positive integers n and k. There are n children numbered from 0 to n - 1 standing in a queue in order from left to right.
Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches either end of the line, i.e. child 0 or child n - 1, the direction of passing is reversed.
Return the number of the child who receives the ball after k seconds.
Example 1:
Input: n = 3, k = 5
Output: 1
Explanation:
| Time elapsed | Children |
|---|---|
0 |
[0, 1, 2] |
1 |
[0, 1, 2] |
2 |
[0, 1, 2] |
3 |
[0, 1, 2] |
4 |
[0, 1, 2] |
5 |
[0, 1, 2] |
Example 2:
Input: n = 5, k = 6
Output: 2
Explanation:
| Time elapsed | Children |
|---|---|
0 |
[0, 1, 2, 3, 4] |
1 |
[0, 1, 2, 3, 4] |
2 |
[0, 1, 2, 3, 4] |
3 |
[0, 1, 2, 3, 4] |
4 |
[0, 1, 2, 3, 4] |
5 |
[0, 1, 2, 3, 4] |
6 |
[0, 1, 2, 3, 4] |
Example 3:
Input: n = 4, k = 2
Output: 2
Explanation:
| Time elapsed | Children |
|---|---|
0 |
[0, 1, 2, 3] |
1 |
[0, 1, 2, 3] |
2 |
[0, 1, 2, 3] |
Constraints:
2 <= n <= 501 <= k <= 50Note: This question is the same as 2582: Pass the Pillow.
Problem summary: You are given two positive integers n and k. There are n children numbered from 0 to n - 1 standing in a queue in order from left to right. Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches either end of the line, i.e. child 0 or child n - 1, the direction of passing is reversed. Return the number of the child who receives the ball after k seconds.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
3 5
5 6
4 2
find-the-losers-of-the-circular-game)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3178: Find the Child Who Has the Ball After K Seconds
class Solution {
public int numberOfChild(int n, int k) {
int mod = k % (n - 1);
k /= (n - 1);
return k % 2 == 1 ? n - mod - 1 : mod;
}
}
// Accepted solution for LeetCode #3178: Find the Child Who Has the Ball After K Seconds
func numberOfChild(n int, k int) int {
mod := k % (n - 1)
k /= (n - 1)
if k%2 == 1 {
return n - mod - 1
}
return mod
}
# Accepted solution for LeetCode #3178: Find the Child Who Has the Ball After K Seconds
class Solution:
def numberOfChild(self, n: int, k: int) -> int:
k, mod = divmod(k, n - 1)
return n - mod - 1 if k & 1 else mod
// Accepted solution for LeetCode #3178: Find the Child Who Has the Ball After K Seconds
fn number_of_child(n: i32, k: i32) -> i32 {
let mut pos = 0;
let mut dir = 1;
for _ in 1..=k {
pos += dir;
if pos == n - 1 {
dir = -1;
} else if pos == 0 {
dir = 1;
}
}
pos
}
fn main() {
let ret = number_of_child(5, 6);
println!("ret={ret}");
}
#[test]
fn test() {
{
let ret = number_of_child(3, 5);
assert_eq!(ret, 1);
}
{
let ret = number_of_child(5, 6);
assert_eq!(ret, 2);
}
{
let ret = number_of_child(4, 2);
assert_eq!(ret, 2);
}
}
// Accepted solution for LeetCode #3178: Find the Child Who Has the Ball After K Seconds
function numberOfChild(n: number, k: number): number {
const mod = k % (n - 1);
k = (k / (n - 1)) | 0;
return k % 2 ? n - mod - 1 : mod;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.