Shortest Pin Path Through Topics
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Pinterest's September 2026 OA hands you rows of pin names, says each row is a clique, and asks for the fewest hops from one pin to another. It looks like a string problem. It's a shortest-path problem on an unweighted graph, so BFS is the answer. If you're taking this in the next day or two, the only real question is how you build the graph without blowing up. StealthCoder sits as a safety net on the live OA if you blank on that part, but the pattern is simple enough to own before you start.
The problem
Each row in topics lists the pins belonging to one topic. Treat every topic as an undirected clique: any two distinct pins in the same topic are connected by one unit-cost edge. Duplicate pin names inside a row have no additional effect. Return the minimum number of pin-to-pin edges from start to destination. Return 0 when they are equal. Return -1 when either endpoint is absent or the destination is unreachable. Function minimumPinSteps(topics: String[][], start: String, destination: String) → int Examples Example 1 topics = [["California","New York"],["New York","Cantonese cuisine"]] start = "California" destination = "Cantonese cuisine" return = 2 California connects to New York, then to Cantonese cuisine. Example 2 topics = [["a","b","c"]] start = "a" destination = "c" return = 1 Pins in one topic are directly adjacent. Constraints 0 <= topics.length <= 500. The total number of pin occurrences is at most 5000. Pin names are nonempty.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to not build the clique edges directly. A topic with 5000 pins would create about 12 million edges. Instead, treat each topic as a hub node. Map every pin to the topics it appears in, then run BFS over pins and topics together. Moving pin to topic to pin counts as one step, so count distance only when you land on a new pin. A cleaner version: when you pop a pin, loop over its topics, and for each unvisited topic, push all its pins at distance + 1, then mark the topic visited so you never scan it twice. Pitfalls: forgetting the edge cases. Return 0 when start equals destination, but only if it exists. Return -1 if either endpoint is absent. Dedupe pins within a row. Skipping the visited-topic mark gives you quadratic blowup. If the hub idea slips away mid-assessment, StealthCoder can surface it live.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Shortest Pin Path Through Topics cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as bus routes. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Pin Path Through Topics FAQ
What's the trick in the Pinterest shortest pin path problem?+
Don't expand cliques into pairwise edges. Map each pin to its topics, then BFS using topics as hubs. Once you expand a topic, mark it visited so its pins are only enqueued once. That keeps the work linear in the total pin occurrences, at most 5000.
Is this BFS or something fancier like Dijkstra?+
Plain BFS. Every edge costs one unit, so the first time you reach the destination is the shortest distance. Dijkstra adds a heap and complexity you don't need. The only twist is handling the clique structure efficiently with topic nodes or visited-topic flags.
What edge cases break most solutions?+
Start equals destination returns 0, but only if the pin actually exists. Either endpoint missing returns -1. Duplicate pins in a row shouldn't matter, so use sets. Empty topics list returns -1 unless the problem logic says otherwise, so check presence in your map first.
How do I prepare for this in 48 hours?+
Write BFS on an adjacency map from memory twice. Then write the hub variant where a group of nodes shares one connection point. Test on the two examples, plus a missing pin, a repeated pin and a disconnected case. That covers nearly everything this problem can throw at you.
What's the time complexity I should state?+
O(P + T) where P is total pin occurrences, at most 5000, and T is the number of topics, at most 500. Building the pin-to-topics map is O(P), and BFS visits each pin once and each topic once. Space is also O(P). Mention that naive clique expansion would be quadratic per topic.