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.
You are given a 0-indexed string word of length n consisting of digits, and a positive integer m.
The divisibility array div of word is an integer array of length n such that:
div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, ordiv[i] = 0 otherwise.Return the divisibility array of word.
Example 1:
Input: word = "998244353", m = 3 Output: [1,1,0,0,0,1,1,0,0] Explanation: There are only 4 prefixes that are divisible by 3: "9", "99", "998244", and "9982443".
Example 2:
Input: word = "1010", m = 10 Output: [0,1,0,1] Explanation: There are only 2 prefixes that are divisible by 10: "10", and "1010".
Constraints:
1 <= n <= 105word.length == nword consists of digits from 0 to 91 <= m <= 109Problem summary: You are given a 0-indexed string word of length n consisting of digits, and a positive integer m. The divisibility array div of word is an integer array of length n such that: div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, or div[i] = 0 otherwise. Return the divisibility array of word.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
"998244353" 3
"1010" 10
subarray-sums-divisible-by-k)make-sum-divisible-by-p)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2575: Find the Divisibility Array of a String
class Solution {
public int[] divisibilityArray(String word, int m) {
int n = word.length();
int[] ans = new int[n];
long x = 0;
for (int i = 0; i < n; ++i) {
x = (x * 10 + word.charAt(i) - '0') % m;
if (x == 0) {
ans[i] = 1;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2575: Find the Divisibility Array of a String
func divisibilityArray(word string, m int) (ans []int) {
x := 0
for _, c := range word {
x = (x*10 + int(c-'0')) % m
if x == 0 {
ans = append(ans, 1)
} else {
ans = append(ans, 0)
}
}
return ans
}
# Accepted solution for LeetCode #2575: Find the Divisibility Array of a String
class Solution:
def divisibilityArray(self, word: str, m: int) -> List[int]:
ans = []
x = 0
for c in word:
x = (x * 10 + int(c)) % m
ans.append(1 if x == 0 else 0)
return ans
// Accepted solution for LeetCode #2575: Find the Divisibility Array of a String
impl Solution {
pub fn divisibility_array(word: String, m: i32) -> Vec<i32> {
let m = m as i64;
let mut x = 0i64;
word.as_bytes()
.iter()
.map(|&c| {
x = (x * 10 + i64::from(c - b'0')) % m;
if x == 0 {
1
} else {
0
}
})
.collect()
}
}
// Accepted solution for LeetCode #2575: Find the Divisibility Array of a String
function divisibilityArray(word: string, m: number): number[] {
const ans: number[] = [];
let x = 0;
for (const c of word) {
x = (x * 10 + Number(c)) % m;
ans.push(x === 0 ? 1 : 0);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.