← 返回博客列表
Robinhood

Robinhood 社招 Phone Screen 真題:Referral Chain Leaderboard(Top 3)

2026-04-01

![Robinhood Referral Leaderboard](/images/robinhood/image copy 2.png)

這題是 Robinhood phone screen 非常典型的「商業語義 + 圖建模」題。

你會拿到兩組陣列:

每筆資料代表一條有向邊:rh_users[i] -> new_users[i]

關鍵定義是:一位使用者不只推薦了直推對象,也推薦了邀請鏈下游所有人

例如: A -> B -> C -> D

目標是輸出推薦榜 Top 3。


規則拆解

Referral Rules

  1. 每位使用者只能被邀請一次。
  2. 使用者加入後不能再次被邀請。
  3. 輸入順序即邀請發生順序。

因此結構可視為 有向森林(無環、每個節點入度 <= 1)。

Leaderboard Rules

  1. 只保留推薦人數 >= 1 的使用者。
  2. 最多回傳 3 位。
  3. 先按推薦人數降序。
  4. 同分按使用者名稱字母序升序。

範例

輸入:

rh_users = ["A", "B", "C"]
new_users = ["B", "C", "D"]

圖結構: A -> B -> C -> D

輸出:

["A 3", "B 2", "C 1"]

建模思路

每個人的分數其實是:

所以做法是:

  1. 建立鄰接表 parent -> children
  2. 用 DFS 後序計算每個節點後代數
  3. 用 memo 避免重複計算
  4. 依規則排序並取前 3

核心計算可線性完成。


Python 解法(Phone Screen 可直接講)

from collections import defaultdict

def top3_referral_leaderboard(rh_users, new_users):
    # 1) Build graph
    graph = defaultdict(list)
    all_users = set(rh_users) | set(new_users)

    for u, v in zip(rh_users, new_users):
        graph[u].append(v)

    # 2) DFS + memo: descendants count (excluding self)
    memo = {}

    def dfs(user):
        if user in memo:
            return memo[user]

        total = 0
        for child in graph[user]:
            total += 1 + dfs(child)

        memo[user] = total
        return total

    # 3) Gather candidates (must be >= 1)
    candidates = []
    for user in all_users:
        cnt = dfs(user)
        if cnt >= 1:
            candidates.append((cnt, user))

    # 4) Sort by count desc, then username asc
    candidates.sort(key=lambda x: (-x[0], x[1]))

    # 5) Top 3 and format
    return [f"{user} {cnt}" for cnt, user in candidates[:3]]


if __name__ == "__main__":
    rh_users = ["A", "B", "C"]
    new_users = ["B", "C", "D"]
    print(top3_referral_leaderboard(rh_users, new_users))
    # ['A 3', 'B 2', 'C 1']

複雜度分析

設邀請邊數為 n

總計:O(n + m log m)


面試怎麼講更加分

  1. 先明確「間接推薦」= 所有後代。
  2. 說清楚結構性質:每個新使用者只出現一次,入度 <= 1。
  3. 反證暴力重掃最壞 O(n^2)
  4. 給出 DFS 後序 + memo,每個節點只算一次。
  5. 補上排序鍵 (-count, username) 與 top-3。

這套回答能同時覆蓋建模能力、複雜度意識、實作落地。


20 秒英文版本(可直接背)

I model referrals as a directed forest and define each user’s score as descendant count. I build adjacency lists in O(n), compute descendant counts with DFS + memoization so each node is processed once, then sort users by (-count, username) and return top 3 with count >= 1.


需要 Robinhood / Stripe / TikTok 社招面試輔導?