Minimum Transfers Between Pin Boards
PinterestSoftware Engineer · OnsiteLast updated
Tests breadth-first search and graph modeling skills by finding the shortest path across overlapping collections without constructing a dense graph between every pair.
Problem Statement
At Pinterest, users organize content into boards. Each board contains a collection of unique pin IDs. You want to navigate from a starting pin source to a destination pin target by jumping between pins that share a common board.
Each time you switch to or take a board, it counts as using one board. You can transition freely between any pins located on the same board once you are on it.
Given a list of boards, where boards[i] is an array of pin IDs on the i-th board, return the minimum number of boards you must visit to travel from source to target. If it is impossible to reach target from source, return -1.
If source == target, you are already at the destination and need 0 boards.
Function Signature
def min_boards(boards: list[list[int]], source: int, target: int) -> int:
pass
Input and Output
boards: A list of lists of integers, whereboards[i]represents all pins on boardi.source: An integer representing the starting pin ID.target: An integer representing the target pin ID.- Return: An integer representing the minimum number of boards used, or
-1if unreachable.
Constraints
1 <= len(boards) <= 10001 <= len(boards[i]) <= 1000- The total number of pins across all boards does not exceed
10^5. - All pin IDs are non-negative integers (
0 <= pin_id <= 10^9). boards[i]contains distinct pin IDs.
Examples
Example 1
boards = [[1, 2, 7], [3, 6, 7]]
source = 1
target = 6
# Output: 2
# Explanation: You take board 0 to go from pin 1 to pin 7, then board 1 to reach pin 6.
Example 2
boards = [[7, 12], [4, 5, 15], [6], [15, 19], [9, 12, 13]]
source = 15
target = 12
# Output: -1
# Explanation: There is no board connection leading from pin 15 to pin 12.
Example 3
boards = [[1, 5], [5, 9]]
source = 5
target = 5
# Output: 0
# Explanation: You are already at pin 5.
Hints
Hint 1
Instead of finding intersections between every pair of boards (which can take O(B^2 * K) time), invert the index to map each pin to the boards that contain it.
Hint 2
Perform a Breadth-First Search (BFS) where each step in the queue represents visiting a board rather than visiting an individual pin.
Hint 3
Keep track of both visited boards and visited pins to avoid reprocessing the same boards and pins multiple times.
Follow-up questions
- How would you return the actual sequence of board IDs used rather than just the minimum count?
- If board transitions have non-uniform costs (e.g., private vs. public boards), how would you adapt your approach?
- How does your algorithm handle a stream of queries where the board collections remain static but source and target vary frequently?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.