Lyft L5 Phone Screen
Anonymous User
1331
We have a few boxes. A box can either be locked or unlocked. Inside any box, you can find
candies, smaller boxes, and keys to other boxes. What is the maximum number of candies 
you can retrieve tracing from an initial box? Write relevant methods, class to solve the problem. 


Examples below,


Example 1:


If you have following boxes, 
box A = {unlocked, 7 candy, boxes=[B, C], keys=[]}
box B = {locked, 5 candy, boxes=[D], keys=[]}
box C = {unlocked, 4 candy, boxes=[], keys=[B] }
box D = {locked, 20 candy, boxes=[], keys=[] }


and you are initially provided box A,


The maximum number of candies you can retrieve are 16 (box A + box B + box C)
Note: You are able to access box D when you visit box B but you don’t have keys to open. 


Example 2:


If you have following boxes, 
box A = {unlocked, 7 candy, boxes=[B, C], keys=[]}
box B = {locked, 5 candy, boxes=[D], keys=[]}
box C = {unlocked, 4 candy, boxes=[], keys=[]}
box D = {locked, 20 candy, boxes=[], keys=[]}


and you are initially provided box A,
The maximum number of candies you can retrieve are 11 (box A + box C)


Example 3:


If you have following boxes, 
box A = {unlocked, 7 candy, boxes=[B, C], keys=[]}
box B = {locked, 5 candy, boxes=[D], keys=[]}
box C = {unlocked, 4 candy, boxes=[], keys=[B, D]}
box D = {locked, 20 candy, boxes=[], keys=[]}


and you are initially provided box A,
The maximum number of candies you can retrieve are 36 (box A + box C + box B + box D)


Example 4:
box A = {locked, 7 candy, boxes=[B, C], keys=[]}
box B = {locked, 5 candy, boxes=[D], keys=[]}
box C = {unlocked, 4 candy, boxes=[], keys=[B, D]}
box D = {locked, 20 candy, boxes=[], keys=[]}


and you are initially provided box A,
The maximum number of candies you can retrieve are 0
Note: box A is locked. 


Example 5:
box A = {unlocked, 7 candy, boxes=[], keys=[]}
box B = {locked, 5 candy, boxes=[D], keys=[]}
box C = {unlocked, 4 candy, boxes=[], keys=[B, D]}
box D = {locked, 20 candy, boxes=[], keys=[]}


and you are initially provided box A,
The maximum number of candies you can retrieve are 7
Note: box A has no keys or boxes


Example 6:
box A = {unlocked, 7 candy, boxes=[], keys=[B, C, A]}
box B = {locked, 5 candy, boxes=[D], keys=[]}
box C = {unlocked, 4 candy, boxes=[], keys=[B, D]}
box D = {locked, 20 candy, boxes=[], keys=[]}


and you are initially provided box A,
The maximum number of candies you can retrieve are 7
Note: box A has keys to box B, box C, box A but not boxes to explore. 
Comments (3)