HardArrayBreadth-First SearchGraphUpdated Sep 2026

1298. Maximum Candies You Can Get from Boxes

Asked at Airbnb

Difficulty

Hard

Time

O(n + total keys + total contain

Space

O(n)

Pattern

Array

Companies

1

Problem

You have n boxes, labeled 0 to n-1. Each box has a status: 1 means it is already open, 0 means it is locked. Each box also holds some candies, a list of keys (labels of boxes those keys unlock), and a list of containedBoxes (other boxes sitting inside it).

You start with a set of boxes given by initialBoxes. You may take the candies from any box you have that is open, or that you hold a key for. When you open a box, you get its candies, plus every key inside it, plus every box inside it. Keys and boxes you collect can then be used to open more boxes.

Return the maximum total number of candies you can collect. Opening a box never hurts you, so the answer is simply the total candies in every box you can eventually reach and unlock.

Boxes, keys and what you can reach

status10100123Green = open, orange = locked but key found, red =locked with no keycandies7541000123We collect 7 + 4 + 5 = 16; the 100 candies in box 3are out of reach
You start with box 0. Box 3 stays locked forever because no key for it exists.

Constraints

  • ●n == status.length == candies.length == keys.length == containedBoxes.length
  • ●1 <= n <= 1000
  • ●status[i] is either 0 or 1
  • ●1 <= candies[i] <= 1000
  • ●0 <= keys[i].length <= n
  • ●0 <= keys[i][j] < n
  • ●All values of keys[i] are unique
  • ●0 <= containedBoxes[i].length <= n

Examples

Example 1
Inputstatus = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
Output16
WhyBox 0 is open, so we take 7 candies and find boxes 1 and 2. Box 1 is locked and we have no key, but box 2 is open: we take 4 candies and find the key to box 1. Now box 1 can be opened, giving 5 more candies and box 3. Box 3 is locked and no key for it exists anywhere, so it stays shut. Total = 7 + 4
Example 2
Inputstatus = [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]
Output6
WhyBox 0 is open. Inside it we find boxes 1 to 5 and also the keys to all of them. Every box becomes openable, so we collect 1 candy from each of the 6 boxes.

Asked At

1 companies
CompanyDifficulty
AirbnbHardView all Airbnb questions →

How to Think About It

1

Order does not matter: opening a box never makes anything worse. So simply keep opening whatever you can until nothing changes.

2

Track two facts per box: have[b] (you possess it) and canOpen[b] (it starts open or you found its key). A box is processable when both are true and you have not opened it yet.

3

Use a queue of processable boxes. When you open a box: collect its candies; for each key inside, mark canOpen and enqueue that box if you already have it; for each box inside, mark have and enqueue it if it can be opened.

4

The "enqueue only when both become true" rule guarantees each box is opened at most once — use an opened flag to be safe.

5

Walkthrough: status = [1,0,1,0], candies = [7,5,4,100], box 0 contains boxes 1 and 2, box 2 holds the key to box 1, box 1 contains box 3. Open 0 (+7) -> get 1 (locked) and 2 (open). Open 2 (+4) -> key to 1, which we already have, so open 1 (+5) -> get box 3, but it is locked and no key exists. Total 16.

Optimal Approach

Step 1: have[b] = true for each initial box; canOpen[b] = (status[b] == 1).
Step 2: Enqueue every initial box with canOpen; mark it opened.
Step 3: While the queue is not empty, pop box b:
total += candies[b]
For each key k in keys[b]: canOpen[k] = true; if have[k] and not opened[k], open and enqueue it.
For each box c in containedBoxes[b]: have[c] = true; if canOpen[c] and not opened[c], open and enqueue it.
Step 4: Return total.

Time: O(n + total keys + total contained boxes). Space: O(n).

BFS: open a box only when you have it AND can open it

start: have[0], canOpen[0] -> open box 0, +7box 0 gives boxes 1, 2box 2 is open -> open it, +4box 2 gives key to box 1 -> open box 1, +5box 1 gives box 3, but no key -> stuckBox 1 was waiting: we had it, but only got its keylater from box 2queue021frontbackBoxes are processed in the order they become openable;total = 7 + 4 + 5 = 16have[b] = you possess box bcanOpen[b] = status[b]==1 or you found its keyenqueue b when have[b] and canOpen[b]
A box enters the queue the moment both conditions become true, so each box is opened once.
ComplexityTime O(n + total keys + total containSpace O(n)Each box is enqueued and opened at most once, and each key and contained box is scanned once when its parent is opened.

What Trips People Up in Real Interviews

1

Only enqueueing boxes when you find them. A locked box you already hold must be enqueued later when its key shows up.

2

Opening a box twice — once when its key appears and once when the box itself appears. Guard with an opened flag.

3

Trying to decide an order of openings. Since opening is monotone (it only gives you more), any order reaches the same final state.

4

Forgetting that keys can arrive before the box does.

Solution Code

O(n + total keys + total contain time
from collections import deque

def maxCandies(status, candies, keys, containedBoxes, initialBoxes):
    n = len(status)
    have = [False] * n
    can_open = [s == 1 for s in status]
    opened = [False] * n
    q = deque()
    for b in initialBoxes:
        have[b] = True
        if can_open[b]:
            opened[b] = True
            q.append(b)
    total = 0
    while q:
        b = q.popleft()
        total += candies[b]
        for k in keys[b]:
            can_open[k] = True
            if have[k] and not opened[k]:
                opened[k] = True
                q.append(k)
        for c in containedBoxes[b]:
            have[c] = True
            if can_open[c] and not opened[c]:
                opened[c] = True
                q.append(c)
    return total

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Maximum Candies You Can Get from Boxes problem?

Maximum Candies You Can Get from Boxes starts you with some boxes; each box is open or locked, holds candies, keys to other boxes, and other boxes. You can open any box you have that is open or for which you hold a key. How many candies can you collect? It is a BFS where a box becomes usable only once two conditions — "I have it" and "I can open it" — are both true.

How do you solve Maximum Candies You Can Get from Boxes?

The optimal approach runs in O(n + total keys + total contain time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Maximum Candies You Can Get from Boxes?

Maximum Candies You Can Get from Boxes is asked at Airbnb. It is a hard difficulty problem.

What are common mistakes on Maximum Candies You Can Get from Boxes?
  • Only enqueueing boxes when you find them. A locked box you already hold must be enqueued later when its key shows up.
  • Opening a box twice — once when its key appears and once when the box itself appears. Guard with an `opened` flag.
  • Trying to decide an order of openings. Since opening is monotone (it only gives you more), any order reaches the same final state.
  • Forgetting that keys can arrive before the box does.