Watch 10 video solutions for Find the Student that Will Replace the Chalk, a medium level problem involving Array, Binary Search, Simulation. This walkthrough by codestorywithMIK has 6,342 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There are n students in a class numbered from 0 to n - 1. The teacher will give each student a problem starting with the student number 0, then the student number 1, and so on until the teacher reaches the student number n - 1. After that, the teacher will restart the process, starting with the student number 0 again.
You are given a 0-indexed integer array chalk and an integer k. There are initially k pieces of chalk. When the student number i is given a problem to solve, they will use chalk[i] pieces of chalk to solve that problem. However, if the current number of chalk pieces is strictly less than chalk[i], then the student number i will be asked to replace the chalk.
Return the index of the student that will replace the chalk pieces.
Example 1:
Input: chalk = [5,1,5], k = 22 Output: 0 Explanation: The students go in turns as follows: - Student number 0 uses 5 chalk, so k = 17. - Student number 1 uses 1 chalk, so k = 16. - Student number 2 uses 5 chalk, so k = 11. - Student number 0 uses 5 chalk, so k = 6. - Student number 1 uses 1 chalk, so k = 5. - Student number 2 uses 5 chalk, so k = 0. Student number 0 does not have enough chalk, so they will have to replace it.
Example 2:
Input: chalk = [3,4,1,2], k = 25 Output: 1 Explanation: The students go in turns as follows: - Student number 0 uses 3 chalk so k = 22. - Student number 1 uses 4 chalk so k = 18. - Student number 2 uses 1 chalk so k = 17. - Student number 3 uses 2 chalk so k = 15. - Student number 0 uses 3 chalk so k = 12. - Student number 1 uses 4 chalk so k = 8. - Student number 2 uses 1 chalk so k = 7. - Student number 3 uses 2 chalk so k = 5. - Student number 0 uses 3 chalk so k = 2. Student number 1 does not have enough chalk, so they will have to replace it.
Constraints:
chalk.length == n1 <= n <= 1051 <= chalk[i] <= 1051 <= k <= 109Problem Overview: Each student consumes a fixed number of chalk pieces from an array while answering questions in order. When the chalk runs out mid-cycle, the student who cannot take their required amount must replace it. The task is to identify that student index efficiently even when k is very large.
Approach 1: Cyclic Reduction using Total Sum (O(n) time, O(1) space)
The key observation: the process repeats in cycles. If the total chalk consumption of one full round is sum(chalk), then after every complete cycle the same sequence repeats. Instead of simulating potentially billions of steps, compute k % totalSum. This reduces the problem to the remaining chalk within a single cycle. Iterate through the array, subtract each student's requirement from the remainder, and return the first index where the remainder becomes smaller than the required chalk. This approach is optimal in practice because it avoids extra memory and only scans the array once.
Approach 2: Binary Search on Prefix Sums (O(n) build + O(log n) query time, O(n) space)
Another strategy models the cumulative chalk usage. Build a prefix sum array where prefix[i] represents the total chalk used by students 0..i. Reduce k using k % totalSum to stay within a single cycle. The goal becomes finding the first prefix value greater than the remaining chalk. Because the prefix array is strictly increasing, apply binary search to locate the smallest index where prefix[i] > remaining. That index corresponds to the student who needs to replace the chalk. This approach is useful when the prefix array may be reused for multiple queries.
Recommended for interviews: The cyclic reduction approach is what most interviewers expect. It demonstrates pattern recognition—identifying a repeating process and eliminating unnecessary simulation. The prefix-sum + binary-search solution shows deeper algorithmic thinking and is useful when handling repeated queries or when cumulative data structures are already present.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Cyclic Reduction using Total Sum | O(n) | O(1) | Best general solution when only one query exists and minimal memory usage is preferred |
| Prefix Sum + Binary Search | O(n) preprocessing + O(log n) search | O(n) | Useful when prefix sums are reused or when practicing binary search on cumulative arrays |