Feed of the newest posts
Write a class Feed for a small social app; Feed(k) sets how many posts a feed shows.
post(user, post_id):userpublishes the postpost_id.follow(follower, followee):followerstarts followingfollowee.unfollow(follower, followee):followerstops followingfollowee.latest(user)returns the ids of thekmost recent posts written byuseror by anyoneuserfollows right now, newest first (fewer if there are fewer).
Users and post ids are integers, and post ids are unique. Following twice, unfollowing someone you don't follow, and following or unfollowing yourself change nothing: you always see your own posts. post, follow and unfollow must be O(1), and the cost of latest must not grow with how many posts those users have written.
The tests call the provided run_feed(k, ops): it builds the feed, applies each operation in order and returns the list of results (None for operations that return nothing, shown as null).
Besides the tests, one large input (10,000 posts, 3,000 feed reads) with a ~500 ms limit (scaled to your device's speed): brute force won't pass.
Examples
run_feed(3, [["post",1,101],["post",2,201],["post",1,102],["latest",1],["follow",1,2],["latest",1],["latest",2]])→[null,null,null,[102,101],null,[102,201,101],[201]]run_feed(2, [["post",1,5],["follow",2,1],["post",2,6],["post",1,7],["latest",2],["unfollow",2,1],["latest",2]])→[null,null,null,null,[7,6],null,[6]]run_feed(2, [["post",1,8],["follow",1,1],["latest",1],["unfollow",1,1],["latest",1]])→[null,null,[8],null,[8]]