Circular Token Distribution on a Ring of Nodes
Arrays & HashingModel answerLast updated
Distribute k tokens clockwise across n nodes in a ring starting at a given index, returning each node's final count without simulating one token at a time.
Problem
You have n nodes arranged in a ring, each holding some number of tokens. Given an array nodes where nodes[i] is the current token count at node i, you need to deliver exactly k new tokens into the ring.
Delivery works like this: starting at start_index, place one token at the current node, then advance clockwise. After the last node (n-1), wrap back to node 0. Repeat until all k tokens have been placed.
Return the state of nodes after all k tokens are delivered.
Function Signature
def deliver_tokens(nodes: List[int], k: int, start_index: int) -> List[int]:
Constraints
1 ≤ n ≤ 10^50 ≤ k ≤ 10^180 ≤ nodes[i] ≤ 10^9
Examples
Example 1
nodes = [1, 2, 3], k = 4, start_index = 1
Step 1 → index 1: [1, 3, 3]
Step 2 → index 2: [1, 3, 4]
Step 3 → index 0: [2, 3, 4]
Step 4 → index 1: [2, 4, 4]
Output: [2, 4, 4]
Example 2
nodes = [0, 0], k = 5, start_index = 0
Output: [3, 2]
What is expected
- A solution that handles
kup to10^18efficiently — a step-by-step simulation will not pass within time limits. - Correct wrap-around behaviour regardless of where
start_indexfalls. - Proper handling of edge cases: empty input,
k = 0,kthat is a multiple ofn, andn = 1.
Hints
Hint 1
Think about what happens after exactly n deliveries. Every node has received the same number of tokens regardless of where you started. Can you use that periodicity to skip most of the work?
Hint 2
Split k into a quotient and a remainder using integer division by n. The quotient tells you how many complete laps happen; the remainder tells you which nodes get one extra token on the final partial lap.
Hint 3
The remainder tokens go to consecutive nodes starting at start_index. Use modulo arithmetic to handle the wrap-around cleanly, and watch out for the empty-array case before you divide by n.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Simulating one token at a time is O(k). With k up to 10^18, that would take billions of steps. The key observation is that the ring has a natural period of n: any n consecutive deliveries touch every node exactly once. That makes the problem decomposable.
Split k with integer division:
full_laps, remainder = divmod(k, n)
- Every node receives exactly
full_lapstokens from the complete laps. - The leftover
remaindertokens go one each to the nextremaindernodes clockwise starting atstart_index.
The total work is O(n), independent of k.
Why this is correct
Token number j (0-indexed) lands on node (start_index + j) mod n. For a given node i, count how many j in [0, k) satisfy that congruence. The k values split into full_laps complete blocks of size n — each block hits node i exactly once — plus a tail of remainder indices starting at start_index. Node i lands in that tail if and only if it is one of the first remainder nodes clockwise from start_index.
So every node gets full_laps tokens, and the nodes in the tail get one extra. This matches the step-by-step simulation exactly.
Checking Example 1: n=3, k=4 → full_laps=1, remainder=1. Adding 1 everywhere gives [2, 3, 4]. One extra token goes to index 1: [2, 4, 4]. ✓
Checking Example 2: n=2, k=5 → full_laps=2, remainder=1. Adding 2 everywhere gives [2, 2]. One extra token goes to index 0: [3, 2]. ✓
Complexity
- Time: O(n) — one pass to apply
full_laps, at mostn-1steps for the remainder. - Space: O(n) for the output copy; O(1) extra work beyond that.
Edge Cases
| Situation | What happens |
|---|---|
| Empty array | Return [] immediately — dividing by n would raise ZeroDivisionError. |
k = 0 | full_laps = remainder = 0; array returned unchanged. |
k < n | full_laps = 0; only the remainder loop runs. |
k divisible by n | remainder = 0; every node gets exactly full_laps. |
n = 1 | All tokens go to the single node; it ends at nodes[0] + k. |
| Large values | Node values can reach ~10^9 + 10^18. Python handles this natively; in Java/C++ use long / int64_t (max ~9.2 × 10^18, sufficient). |
start_index out of range | Reduce with % n as a cheap defensive step before the remainder loop. |
Solution
from typing import List
def deliver_tokens(nodes: List[int], k: int, start_index: int) -> List[int]:
n = len(nodes)
if n == 0:
return []
result = list(nodes) # work on a copy; don't mutate the input
full_laps, remainder = divmod(k, n)
# Every node receives tokens from each complete lap
if full_laps:
for i in range(n):
result[i] += full_laps
# Distribute the leftover tokens clockwise from start_index
start = start_index % n
for j in range(remainder):
result[(start + j) % n] += 1
return result
Common Mistakes
Naive simulation. Writing while k > 0: ... k -= 1 passes the examples but times out on any test where k is in the billions. This is almost always the first approach people try — recognize it, then step back and look for the period.
Forgetting the empty-array guard. divmod(k, 0) raises an exception. The guard needs to come before the division.
Off-by-one on start_index. Forgetting to reduce start_index modulo n before the remainder loop causes an index out of bounds when start_index ≥ n.
Follow-up: Each node has a capacity limit
If each node i can hold at most cap[i] tokens, the delivery pointer must skip full nodes. Let spare room c[i] = cap[i] - nodes[i].
After t complete laps, node i has absorbed min(c[i], t) new tokens, so total placed is S(t) = Σ min(c[i], t). This function is non-decreasing in t. Binary search for the largest t with S(t) ≤ k in O(n log(max c)), then hand out the remaining k − S(t) tokens one by one to nodes with c[i] > t, walking clockwise from start_index. This is the classic water-filling pattern.