← Questions
Maximum candies from boxes with keys
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Airbnb's technical phone screen, last asked May 2026. It tests bfs, simulation and graphs.
There are n boxes. Each box is open or locked and holds some candies, keys to other boxes, and other boxes inside it. You start holding a few boxes. You may open a box you hold if it is open or you have its key; opening it gives you its candies, keys and inner boxes. Return the most candies you can collect.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
max_candies(status, candies, keys, contained_boxes, initial_boxes) -> int, wherestatus[i]is 1 if boxiis open,candies[i]its candies,keys[i]the boxes its keys open,contained_boxes[i]the boxes inside it, andinitial_boxesthe boxes you start with.
Example
status = [1, 0, 1, 0]
candies = [7, 5, 4, 100]
keys = [[], [], [1], []]
contained_boxes = [[1, 2], [3], [], []]
initial_boxes = [0]
max_candies(...) -> 16
# open box 0 (7): get boxes 1 (locked) and 2 (open)
# open box 2 (4): get the key to box 1
# open box 1 (5): get box 3, which stays locked
Constraints
-
1 <= n <= 1000; each box appears inside at most one other box. - Every index refers to a valid box.
Clarifying questions
- Can a key arrive before the box it opens, or after?
- Can a box be contained in more than one box?
- Is a key used up when it opens a box?
What a strong answer covers
- A BFS queue of boxes you can open now, plus sets for boxes held, keys held and boxes opened.
- The case that trips people up: holding a locked box whose key turns up later, so a new key must re-check held boxes (and a new box must check held keys).
- Each box is opened at most once: O(n + total keys + total contained boxes).
- Tests: a key found before its box, a box whose key never appears, everything open from the start.
Follow-ups
- Model it as classes (
Box,Inventory) and explain the design. - Opening a box costs time: maximize candies within a time limit.
- Return the order in which the boxes were opened.
Where it's asked
- Airbnb · Technical phone screen · Last asked May 202645 min · One algorithm problem from a small recurring pool
What it tests
bfs · simulation · graphs
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
