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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where:
status[i] is 1 if the ith box is open and 0 if the ith box is closed,candies[i] is the number of candies in the ith box,keys[i] is a list of the labels of the boxes you can open after opening the ith box.containedBoxes[i] is a list of the boxes you found inside the ith box.You are given an integer array initialBoxes that contains the labels of the boxes you initially have. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it.
Return the maximum number of candies you can get following the rules above.
Example 1:
Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you do not have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy.
Example 2:
Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6.
Constraints:
n == status.length == candies.length == keys.length == containedBoxes.length1 <= n <= 1000status[i] is either 0 or 1.1 <= candies[i] <= 10000 <= keys[i].length <= n0 <= keys[i][j] < nkeys[i] are unique.0 <= containedBoxes[i].length <= n0 <= containedBoxes[i][j] < ncontainedBoxes[i] are unique.0 <= initialBoxes.length <= n0 <= initialBoxes[i] < nProblem summary: You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where: status[i] is 1 if the ith box is open and 0 if the ith box is closed, candies[i] is the number of candies in the ith box, keys[i] is a list of the labels of the boxes you can open after opening the ith box. containedBoxes[i] is a list of the boxes you found inside the ith box. You are given an integer array initialBoxes that contains the labels of the boxes you initially have. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,0,1,0] [7,5,4,100] [[],[],[1],[]] [[1,2],[3],[],[]] [0]
[1,0,0,0,0,0] [1,1,1,1,1,1] [[1,2,3,4,5],[],[],[],[],[]] [[1,2,3,4,5],[],[],[],[],[]] [0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1298: Maximum Candies You Can Get from Boxes
class Solution {
public int maxCandies(
int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
Deque<Integer> q = new ArrayDeque<>();
Set<Integer> has = new HashSet<>();
Set<Integer> took = new HashSet<>();
int ans = 0;
for (int box : initialBoxes) {
has.add(box);
if (status[box] == 1) {
q.offer(box);
took.add(box);
ans += candies[box];
}
}
while (!q.isEmpty()) {
int box = q.poll();
for (int k : keys[box]) {
if (status[k] == 0) {
status[k] = 1;
if (has.contains(k) && !took.contains(k)) {
q.offer(k);
took.add(k);
ans += candies[k];
}
}
}
for (int b : containedBoxes[box]) {
has.add(b);
if (status[b] == 1 && !took.contains(b)) {
q.offer(b);
took.add(b);
ans += candies[b];
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1298: Maximum Candies You Can Get from Boxes
func maxCandies(status []int, candies []int, keys [][]int, containedBoxes [][]int, initialBoxes []int) (ans int) {
q := []int{}
has := make(map[int]bool)
took := make(map[int]bool)
for _, box := range initialBoxes {
has[box] = true
if status[box] == 1 {
q = append(q, box)
took[box] = true
ans += candies[box]
}
}
for len(q) > 0 {
box := q[0]
q = q[1:]
for _, k := range keys[box] {
if status[k] == 0 {
status[k] = 1
if has[k] && !took[k] {
q = append(q, k)
took[k] = true
ans += candies[k]
}
}
}
for _, b := range containedBoxes[box] {
has[b] = true
if status[b] == 1 && !took[b] {
q = append(q, b)
took[b] = true
ans += candies[b]
}
}
}
return
}
# Accepted solution for LeetCode #1298: Maximum Candies You Can Get from Boxes
class Solution:
def maxCandies(
self,
status: List[int],
candies: List[int],
keys: List[List[int]],
containedBoxes: List[List[int]],
initialBoxes: List[int],
) -> int:
q = deque()
has, took = set(initialBoxes), set()
ans = 0
for box in initialBoxes:
if status[box]:
q.append(box)
took.add(box)
ans += candies[box]
while q:
box = q.popleft()
for k in keys[box]:
if not status[k]:
status[k] = 1
if k in has and k not in took:
q.append(k)
took.add(k)
ans += candies[k]
for b in containedBoxes[box]:
has.add(b)
if status[b] and b not in took:
q.append(b)
took.add(b)
ans += candies[b]
return ans
// Accepted solution for LeetCode #1298: Maximum Candies You Can Get from Boxes
use std::collections::{HashSet, VecDeque};
impl Solution {
pub fn max_candies(
mut status: Vec<i32>,
candies: Vec<i32>,
keys: Vec<Vec<i32>>,
contained_boxes: Vec<Vec<i32>>,
initial_boxes: Vec<i32>,
) -> i32 {
let mut q: VecDeque<i32> = VecDeque::new();
let mut has: HashSet<i32> = HashSet::new();
let mut took: HashSet<i32> = HashSet::new();
let mut ans = 0;
for &box_ in &initial_boxes {
has.insert(box_);
if status[box_ as usize] == 1 {
q.push_back(box_);
took.insert(box_);
ans += candies[box_ as usize];
}
}
while let Some(box_) = q.pop_front() {
for &k in &keys[box_ as usize] {
if status[k as usize] == 0 {
status[k as usize] = 1;
if has.contains(&k) && !took.contains(&k) {
q.push_back(k);
took.insert(k);
ans += candies[k as usize];
}
}
}
for &b in &contained_boxes[box_ as usize] {
has.insert(b);
if status[b as usize] == 1 && !took.contains(&b) {
q.push_back(b);
took.insert(b);
ans += candies[b as usize];
}
}
}
ans
}
}
// Accepted solution for LeetCode #1298: Maximum Candies You Can Get from Boxes
function maxCandies(
status: number[],
candies: number[],
keys: number[][],
containedBoxes: number[][],
initialBoxes: number[],
): number {
const q: number[] = [];
const has: Set<number> = new Set();
const took: Set<number> = new Set();
let ans = 0;
for (const box of initialBoxes) {
has.add(box);
if (status[box] === 1) {
q.push(box);
took.add(box);
ans += candies[box];
}
}
while (q.length > 0) {
const box = q.pop()!;
for (const k of keys[box]) {
if (status[k] === 0) {
status[k] = 1;
if (has.has(k) && !took.has(k)) {
q.push(k);
took.add(k);
ans += candies[k];
}
}
}
for (const b of containedBoxes[box]) {
has.add(b);
if (status[b] === 1 && !took.has(b)) {
q.push(b);
took.add(b);
ans += candies[b];
}
}
}
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.