call(back)
Algorithms & Data StructureshardSign in to star problems

Collect Pins from Reachable Boards

On Pinterest, a board contains pins, and the same pin can be saved to many boards. From any board you can "hop" to any other board that shares at least one pin with it.

Given a mapping boards (board id to the pin ids on that board) and a starting board, return all pins reachable from the start — i.e., every pin on every board in the start board's connected component.

Example:

boards = {
  "B1": ["p1", "p2"],
  "B2": ["p2", "p3"],     // reachable from B1 via shared p2
  "B3": ["p4"],           // not reachable
  "B4": ["p3", "p5"],     // reachable from B2 via shared p3
  "B5": [],
}
collectReachablePins(boards, "B1")  ->  ["p1", "p2", "p3", "p5"]

For the judge, return the reachable pins as a sorted array. A missing start board returns an empty array.

Hints

Hint 1

Do not build board-to-board edges — every pair of boards sharing a pin would get one, which blows up on popular pins. Traverse board -> its pins -> their boards instead, using an inverted pin-to-boards index.

Hint 2

Mark both pins and boards as visited. The visited-pin set is what keeps one pin saved to thousands of boards from being re-expanded each time.

Asked at