Nooks VO 面试真题解析:Wikipedia 页面最短链接距离(Degree of Separation)

18次阅读
没有评论

Implement a program that finds the shortest path of links between one Wikipedia URL and another.

You can imagine the final result is something similar to https://www.sixdegreesofwikipedia.com/

Part 1 – pass test cases 1-3B (crawl distance < 3)

Part 2 – pass test cases 3C-4B (crawl distance 3)

Don’t worry about input validation or parsing – assume the input will always be a valid Wikipedia page name.

async def find_degree_of_separation(page1: str, page2: str) -> float:

Return the shortest directed path between one article and another.
Return -1 if either article is not a valid Wikipedia article or there is no connection between them.
Assume that the longest distance between any two Wikipedia articles is 6.

这道题要求实现一个异步函数,计算两个 Wikipedia 页面之间的最短“链接跳数”,本质上是一个有向图上的最短路径问题。每个页面是一个节点,页面中的内部链接是边,目标是从 page1 走到 page2 的最少点击次数;如果两页相同返回 0,如果没有路径则返回 -1。数据规模和测试重点暗示需要用 BFS 做分层搜索,并且通过缓存 / 去重避免重复抓取页面链接;题目还提到只需处理到 6 跳以内的页面关系,因此搜索深度可以设置上限。

正文完
 0