M5-019Medium去重与关联 · Dedup & joins

如何找出互为好友的用户对(A 关注 B 且 B 关注 A)?

How do you find pairs of users who follow each other?
💡 考场高分一句话答案:

自连接同一张关注表,条件为 a.user = b.friend AND b.user = a.friend,并用 a.user < b.user 去重对称对。

Self-join the follow table on both directions, then keep only a.user < b.user to remove the symmetric duplicate.

📌 核心考察要点 (Key Points)

  • 自连接:两次 join 同一表
  • 去重对称:加 a.user < b.user 只保留一个方向
  • 性能:大表先过滤活跃用户或分区
  • 扩展:共同好友数、二度人脉
  • Self-join: two joins on the same table
  • Symmetry removal: the a.user < b.user predicate keeps one direction only
  • Performance: filter to active users or partition first
  • Extensions: mutual-friend counts, second-degree connections

🔍 大厂面试官连环追问 (Follow-ups)

  • 如何计算每对好友的共同好友数?
  • 如何处理重复关注记录?

嫌字多看太累?进入苏格拉底式对话推演实验室

零打字负担,以大厂考官真实困境切入,通过点选直觉选项暴露思维盲区,手把手推导 Why。

💬 开启对话式破冰 ➔
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔