Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[5,1,5] 22
[3,4,1,2] 25
pass-the-pillow)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1894: Find the Student that Will Replace the Chalk
class Solution {
public int chalkReplacer(int[] chalk, int k) {
long s = 0;
for (int x : chalk) {
s += x;
}
k %= s;
for (int i = 0;; ++i) {
if (k < chalk[i]) {
return i;
}
k -= chalk[i];
}
}
}
// Accepted solution for LeetCode #1894: Find the Student that Will Replace the Chalk
func chalkReplacer(chalk []int, k int) int {
s := 0
for _, x := range chalk {
s += x
}
k %= s
for i := 0; ; i++ {
if k < chalk[i] {
return i
}
k -= chalk[i]
}
}
# Accepted solution for LeetCode #1894: Find the Student that Will Replace the Chalk
class Solution:
def chalkReplacer(self, chalk: List[int], k: int) -> int:
s = sum(chalk)
k %= s
for i, x in enumerate(chalk):
if k < x:
return i
k -= x
// Accepted solution for LeetCode #1894: Find the Student that Will Replace the Chalk
impl Solution {
pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
let mut s: i64 = chalk.iter().map(|&x| x as i64).sum();
let mut k = (k as i64) % s;
for (i, &x) in chalk.iter().enumerate() {
if k < (x as i64) {
return i as i32;
}
k -= x as i64;
}
0
}
}
// Accepted solution for LeetCode #1894: Find the Student that Will Replace the Chalk
function chalkReplacer(chalk: number[], k: number): number {
const s = chalk.reduce((acc, cur) => acc + cur, 0);
k %= s;
for (let i = 0; ; ++i) {
if (k < chalk[i]) {
return i;
}
k -= chalk[i];
}
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.