Reported December 2025
HackerRankmath

Ball Passing at Time K

Reported by candidates from HackerRank's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live HackerRank OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this HackerRank question is treating k as the number of throws. It's not. Time k means k-1 throws, and one off-by-one fails the hidden tests. This one was reported in December 2025, and it looks easy until you see k goes up to 10^18. You can't simulate that. It's a functional graph walk with cycle detection, and you need the pattern before you start typing. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a way out while the proctor sees nothing.

The problem

There are n friends numbered from 1 to n. You are given an array throwsTo, where throwsTo[i - 1] is the friend who receives the ball whenever friend i throws it.
Time is 1-indexed. At time 1, before any throws happen, friend 1 is holding the ball. To move from time t to time t + 1, the current holder throws the ball once to the friend indicated by throwsTo. Return the friend holding the ball at time k.
Equivalently, when answering time k, exactly k - 1 throws have occurred. Do not interpret k as the number of throws.

Function
friendHoldingBall(n: int, throwsTo: int[], k: long) → int

Examples
Example 1
n = 6
throwsTo = [2,3,4,5,3,6]
k = 10
return = 4
At time 1, friend 1 has the ball. After each throw, the holders at times 1 through 10 are 1, 2, 3, 4, 5, 3, 4, 5, 3, 4. Therefore at time 10, after 9 throws, friend 4 holds the ball.
Example 2
n = 3
throwsTo = [2,3,1]
k = 1
return = 1
At time 1, no throw has happened yet, so the initial holder friend 1 is returned.
Example 3
n = 4
throwsTo = [2,2,4,3]
k = 5
return = 2
Friend 1 throws to friend 2, and friend 2 throws to themself, so friend 2 holds the ball at every time after 1.

Constraints
n == throwsTo.length
1 <= n <= 2 * 10^5
1 <= throwsTo[i] <= n
1 <= k <= 10^18

Reported by candidates. Source: FastPrep

Pattern and pitfall

Each friend points to exactly one other friend, so the path from friend 1 is a rho shape: a tail, then a cycle. Walk from friend 1 and record the first time each friend is visited in an array or hash map. Stop when you hit a friend you've already seen. Now you know the tail length and the cycle length. Let steps = k-1. If steps is less than the number of visited nodes, return the node at that index directly. Otherwise, subtract the tail length, take the remainder modulo the cycle length, and index into the cycle. The pitfall is the off-by-one on k, plus self-loops like Example 3, which are just cycles of length 1. Use 64-bit math for k. Total work is O(n). If the cycle math gets tangled live, StealthCoder is the hedge that gets you the clean version.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Ball Passing at Time K cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass HackerRank's OA.

HackerRank reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Ball Passing at Time K FAQ

What's the trick to Ball Passing at Time K?+

Don't simulate. k can reach 10^18, but the walk from friend 1 must enter a cycle within n steps. Record visit order, find where the cycle starts, then use modulo on the remaining steps to jump straight to the answer in O(n).

Is k the number of throws?+

No. At time 1 nobody has thrown yet, so friend 1 holds the ball. At time k exactly k-1 throws have happened. Set steps = k - 1 first and work with that. Mixing these up is the most common reason the first submission fails.

How do I handle self-loops like in Example 3?+

A self-loop is a cycle of length 1. Your general cycle logic already covers it. Friend 2 points to friend 2, so the first repeat is detected immediately, the cycle length is 1, and the modulo always lands on friend 2.

What data types do I need?+

k goes up to 10^18, so use a 64-bit integer for k and steps. The visit-order array and indices fit in normal ints since n is at most 2 * 10^5. In Python this is free, but in Java or C++ use long.

How do I prepare for this in 48 hours?+

Practice the rho-shaped walk once: mark first-visit times, detect the repeat, compute tail and cycle length, then apply modulo. Test your code on all three examples, especially k = 1 and the self-loop case. That covers the edge cases this problem is built around.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with HackerRank.

OA at HackerRank?
Invisible during screen share
Get it