← Yahoo Interview Insights

Yahoo·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Yahoo Data Scientist interview that was basically a graph theory problem dressed up as a string puzzle. Took me a while to even see what they were going for, and the implementation details got messy fast.

Questions Asked (1)

Q1

Given a list of lowercase words, merge them into one string by chaining words where the last character of the current string matches the first character of the next word, without duplicating the shared character. You can reorder words freely. Return any valid merged string using all words exactly once, or 'IMPOSSIBLE' if none exists. How would you design a near-linear time algorithm for this, and how do you handle edge cases like self-loops and disconnected components?

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

I stared at the examples for longer than I should have before I realized this is just an Eulerian path problem on a 26-node directed graph where each word is an edge from its first letter to its last.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as finding an Eulerian trail in a directed multigraph where each word is an edge from its first to last character. Use Hierholzer's algorithm to find the trail in O(V+E) time, then reconstruct the merged string by overlapping characters. Address edge cases like self-loops, disconnected components, and degree conditions explicitly.

Pro tip: Emphasize that the graph has at most 26 vertices (letters), so the algorithm is effectively linear in the number of words. Also, mention that you would validate the degree conditions before running Hierholzer to quickly return 'IMPOSSIBLE'.

1. Graph Modeling

Represent each word as a directed edge from its first character to its last character. Note that multiple edges (words) between the same pair of vertices are allowed, forming a multigraph.

2. Eulerian Trail Conditions

Check that the graph is weakly connected (ignoring isolated vertices) and that the degree conditions for an Eulerian trail are met: at most one vertex with out-degree - in-degree = 1 (start), at most one with in-degree - out-degree = 1 (end), and all others balanced.

3. Hierholzer's Algorithm

If conditions hold, run Hierholzer's algorithm to find an Eulerian trail. Use a stack to perform a depth-first traversal, splicing cycles together, ensuring O(V+E) time.

4. String Reconstruction

Traverse the Eulerian trail in order, appending each word's characters except the first (since it overlaps with the previous word's last character). Handle the first word specially by appending all its characters.

5. Edge Case Handling

Explicitly discuss self-loops (words with same first and last character) which contribute equally to in and out degrees and are naturally handled. For disconnected components, ensure all edges belong to a single weakly connected component; otherwise return 'IMPOSSIBLE'.

Key Points to Mention

  • Eulerian trail in directed multigraph and its existence conditions (degree balance and connectivity).
  • Hierholzer's algorithm for O(V+E) time complexity, with V ≤ 26 so effectively O(E).
  • Handling of self-loops: they are edges from a vertex to itself and do not affect degree balance.
  • Connectivity check: all vertices with non-zero degree must be in the same weakly connected component.
  • Reconstruction step: overlapping characters correctly, especially for the first word and when words are single characters.
  • Edge cases: empty list, single word, multiple disconnected components, and cases where degree conditions fail.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.