KatInterview

Batch Processor for a Directed Follow Graph

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designhash-mapsetgraph

Last updated

Simulate a sequence of follow, unfollow, and query operations on a directed user graph, keeping outgoing and incoming edge views consistent and returning sorted results.

Follow Graph Simulator

You are building the core of a "who follows whom" service. Relationships are directed: if user a follows user b, that says nothing about whether b follows a.

Rather than writing a class, you will implement a single function that replays a list of operations against an initially empty graph and reports the outcome of each one.

Input

  • operations: a list of strings. Each string is one of "follow", "unfollow", "get_following", "get_followers", "is_following".
  • arguments: a list of the same length as operations. The entry at index i holds the integer arguments for operations[i].

Operations

Process the operations in order. For each operation, append exactly one value to the result list.

OperationArgumentsEffectValue appended
follow[follower, followee]Adds the directed edge follower -> followee. If the edge is already present, nothing changes.None
unfollow[follower, followee]Removes the edge follower -> followee. If the edge is not present, nothing changes.None
get_following[user]Looks up every user that user currently follows.A list of those user IDs in ascending order
get_followers[user]Looks up every user that currently follows user.A list of those user IDs in ascending order
is_following[follower, followee]Checks whether the edge follower -> followee currently exists.True or False

Rules and edge cases

  • A user may follow themselves. Such a self-edge is a normal edge: it appears in both that user's following list and their followers list, and unfollow removes it.
  • Repeating follow for an existing edge never creates duplicates.
  • Calling unfollow for an edge that does not exist (including when either user has never been seen) does nothing and is not an error.
  • Querying a user who has no edges, or who has never appeared, returns an empty list for the two list queries and False for is_following.
  • Lists returned by get_following and get_followers must be sorted ascending and contain no duplicates.

Output

Return a list with one entry per operation, in the same order as the input.

Constraints

  • len(operations) == len(arguments).
  • All user IDs are positive integers.
  • The number of users and edges can be very large, so each follow, unfollow, and is_following should take roughly constant time on average, and no operation should scan all users.
  • Both list queries should only do work proportional to the size of the answer (plus sorting it).

Examples

Example 1

Input: run_follow_graph_ops(['follow', 'follow', 'follow', 'get_following', 'get_followers', 'is_following'], [[1, 2], [1, 3], [2, 1], [1], [1], [1, 2]])

Output: [None, None, None, [2, 3], [2], True]

Example 2

Input: run_follow_graph_ops(['get_following', 'get_followers', 'is_following'], [[99], [99], [99, 100]])

Output: [[], [], False]

Example 3

Input: run_follow_graph_ops(['follow', 'follow', 'follow', 'unfollow', 'get_following', 'get_followers'], [[5, 5], [5, 7], [5, 7], [5, 5], [5], [5]])

Output: [None, None, None, None, [7], []]

Hints

Hint 1

A single mapping from each user to the users they follow makes get_followers require scanning everyone. Think about what second view you need.

Hint 2

Store two dictionaries of sets: one for outgoing edges and one for incoming edges. Sets give constant-time insert, delete, and membership checks and make repeated follows harmless.

Hint 3

Every change must update both dictionaries together, and unfollow should only touch them if the edge actually exists. Use .get with an empty default for lookups so unknown users return [] or False, and sort only when returning lists.