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) array of positive integers candiesCount where candiesCount[i] represents the number of candies of the ith type you have. You are also given a 2D array queries where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi].
You play a game with the following rules:
0.i unless you have eaten all candies of type i - 1.Construct a boolean array answer such that answer.length == queries.length and answer[i] is true if you can eat a candy of type favoriteTypei on day favoriteDayi without eating more than dailyCapi candies on any day, and false otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.
Return the constructed array answer.
Example 1:
Input: candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]] Output: [true,false,true] Explanation: 1- If you eat 2 candies (type 0) on day 0 and 2 candies (type 0) on day 1, you will eat a candy of type 0 on day 2. 2- You can eat at most 4 candies each day. If you eat 4 candies every day, you will eat 4 candies (type 0) on day 0 and 4 candies (type 0 and type 1) on day 1. On day 2, you can only eat 4 candies (type 1 and type 2), so you cannot eat a candy of type 4 on day 2. 3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13.
Example 2:
Input: candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]] Output: [false,true,true,false,false]
Constraints:
1 <= candiesCount.length <= 1051 <= candiesCount[i] <= 1051 <= queries.length <= 105queries[i].length == 30 <= favoriteTypei < candiesCount.length0 <= favoriteDayi <= 1091 <= dailyCapi <= 109Problem summary: You are given a (0-indexed) array of positive integers candiesCount where candiesCount[i] represents the number of candies of the ith type you have. You are also given a 2D array queries where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi]. You play a game with the following rules: You start eating candies on day 0. You cannot eat any candy of type i unless you have eaten all candies of type i - 1. You must eat at least one candy per day until you have eaten all the candies. Construct a boolean array answer such that answer.length == queries.length and answer[i] is true if you can eat a candy of type favoriteTypei on day favoriteDayi without eating more than dailyCapi candies on any day, and false otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2. Return the constructed array answer.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[7,4,5,3,8] [[0,2,2],[4,2,4],[2,13,1000000000]]
[5,2,6,4,1] [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1744: Can You Eat Your Favorite Candy on Your Favorite Day?
class Solution {
public boolean[] canEat(int[] candiesCount, int[][] queries) {
int n = candiesCount.length;
long[] s = new long[n + 1];
for (int i = 0; i < n; ++i) {
s[i + 1] = s[i] + candiesCount[i];
}
int m = queries.length;
boolean[] ans = new boolean[m];
for (int i = 0; i < m; ++i) {
int t = queries[i][0], day = queries[i][1], mx = queries[i][2];
long least = day, most = (long) (day + 1) * mx;
ans[i] = least < s[t + 1] && most > s[t];
}
return ans;
}
}
// Accepted solution for LeetCode #1744: Can You Eat Your Favorite Candy on Your Favorite Day?
func canEat(candiesCount []int, queries [][]int) (ans []bool) {
n := len(candiesCount)
s := make([]int, n+1)
for i, v := range candiesCount {
s[i+1] = s[i] + v
}
for _, q := range queries {
t, day, mx := q[0], q[1], q[2]
least, most := day, (day+1)*mx
ans = append(ans, least < s[t+1] && most > s[t])
}
return
}
# Accepted solution for LeetCode #1744: Can You Eat Your Favorite Candy on Your Favorite Day?
class Solution:
def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
s = list(accumulate(candiesCount, initial=0))
ans = []
for t, day, mx in queries:
least, most = day, (day + 1) * mx
ans.append(least < s[t + 1] and most > s[t])
return ans
// Accepted solution for LeetCode #1744: Can You Eat Your Favorite Candy on Your Favorite Day?
struct Solution;
impl Solution {
fn can_eat(candies_count: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<bool> {
let mut prefix: Vec<i64> = vec![0];
let mut prev = 0;
for count in candies_count {
prev += count as i64;
prefix.push(prev);
}
let mut res = vec![];
for q in queries {
let t = q[0] as usize;
let d = q[1] as i64;
let c = q[2] as i64;
res.push(prefix[t] / c <= d && d < prefix[t + 1]);
}
res
}
}
#[test]
fn test() {
let candies_count = vec![7, 4, 5, 3, 8];
let queries = vec_vec_i32![[0, 2, 2], [4, 2, 4], [2, 13, 1000000000]];
let res = vec![true, false, true];
assert_eq!(Solution::can_eat(candies_count, queries), res);
let candies_count = vec![5, 2, 6, 4, 1];
let queries = vec_vec_i32![[3, 1, 2], [4, 10, 3], [3, 10, 100], [4, 100, 30], [1, 3, 1]];
let res = vec![false, true, true, false, false];
assert_eq!(Solution::can_eat(candies_count, queries), res);
let candies_count = vec![7, 11, 5, 3, 8];
let queries = vec_vec_i32![[2, 2, 6], [4, 2, 4], [2, 13, 1000000000]];
let res = vec![false, false, true];
assert_eq!(Solution::can_eat(candies_count, queries), res);
}
// Accepted solution for LeetCode #1744: Can You Eat Your Favorite Candy on Your Favorite Day?
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1744: Can You Eat Your Favorite Candy on Your Favorite Day?
// class Solution {
// public boolean[] canEat(int[] candiesCount, int[][] queries) {
// int n = candiesCount.length;
// long[] s = new long[n + 1];
// for (int i = 0; i < n; ++i) {
// s[i + 1] = s[i] + candiesCount[i];
// }
// int m = queries.length;
// boolean[] ans = new boolean[m];
// for (int i = 0; i < m; ++i) {
// int t = queries[i][0], day = queries[i][1], mx = queries[i][2];
// long least = day, most = (long) (day + 1) * mx;
// ans[i] = least < s[t + 1] && most > s[t];
// }
// 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.