系统设计面试题 - 为一个社交网络设计数据结构

2018-04-26  本文已影响447人  专职跑龙套

引用:
系统设计入门

为一个社交网络设计数据结构

解答

第一步:通过讨论,明确限制及用例,确定Scope

支持的用例:

不支持的用例:

Constraints and assumptions:

计算规模:

第二步:高层次设计

为一个社交网络设计数据结构

第三步:设计核心组件

如果不考虑连接关系的存储问题,可以通过图的广度优先搜索来查找最短路径。
参见:数据结构 图的表示及相关算法&LeetCode题目

现假设用户信息存储在不同的机器上。
User Graph Service 的主要工作:

class UserGraphService(object):

    def __init__(self, lookup_service):
        self.lookup_service = lookup_service

    def person(self, person_id):
        person_server = self.lookup_service.lookup_person_server(person_id)
        return person_server.people([person_id])

    def shortest_path(self, source_key, dest_key):
        if source_key is None or dest_key is None:
            return None
        if source_key is dest_key:
            return [source_key]
        prev_node_keys = self._shortest_path(source_key, dest_key)
        if prev_node_keys is None:
            return None
        else:
            # Iterate through the path_ids backwards, starting at dest_key
            path_ids = [dest_key]
            prev_node_key = prev_node_keys[dest_key]
            while prev_node_key is not None:
                path_ids.append(prev_node_key)
                prev_node_key = prev_node_keys[prev_node_key]
            # Reverse the list since we iterated backwards
            return path_ids[::-1]

    def _shortest_path(self, source_key, dest_key, path):
        # Use the id to get the Person
        source = self.person(source_key)
        # Update our bfs queue
        queue = deque()
        queue.append(source)
        # prev_node_keys keeps track of each hop from
        # the source_key to the dest_key
        prev_node_keys = {source_key: None}
        # We'll use visited_ids to keep track of which nodes we've
        # visited, which can be different from a typical bfs where
        # this can be stored in the node itself
        visited_ids = set()
        visited_ids.add(source.id)
        while queue:
            node = queue.popleft()
            if node.key is dest_key:
                return prev_node_keys
            prev_node = node
            for friend_id in node.friend_ids:
                if friend_id not in visited_ids:
                    friend_node = self.person(friend_id)
                    queue.append(friend_node)
                    prev_node_keys[friend_id] = prev_node.key
                    visited_ids.add(friend_id)
        return None

第四步:扩展设计

为一个社交网络设计数据结构
上一篇 下一篇

猜你喜欢

热点阅读