Feed page walk
EasyOnline Assessment
Replay a stream of posts, deletes and page reads on a social feed that uses offset pagination, and report what each read shows.
The feed lists posts newest first. Each post has an ID.
"post ID"adds a post to the top of the feed. If a post with that ID is already in the feed, it is moved to the top instead (never listed twice)."delete ID"removes the post with that ID; if there is no such post, nothing happens."page K"reads pageK(1-based) of the feed as it is right now: the posts at positions(K-1) × page_sizeup toK × page_size - 1, counting from the top. A page past the end of the feed is empty; the last page may be short."next"reads the page after the one the reader read most recently, again against the current feed. The reader starts at page 0, so the firstnextreads page 1. Both"page K"and"next"update the reader's position to the page they read, even when that page is empty; pages never wrap around."restart"puts the reader back at page 0 and reads nothing.
Input: page_size, an integer, and ops, a list of strings in the formats above.
Output: a list with one entry for every page and next op, in order. Each entry is the list of IDs on that page from top to bottom, for example ["c", "b"]; an empty page is [].
Example 1
- Input:
- [2,["post a","post b","post c","next","post d","next","delete a","next","restart","next"]]
- Output:
- [["c","b"],["b","a"],[],["d","c"]]
- Explanation
- With page size 2, posting a, b, c gives the feed c, b, a. The first
nextis page 1: [c, b]. After d is posted the feed is d, c, b, a, so the secondnext(page 2) shows [b, a]: b appears again because it was pushed down. After deleting a the feed is d, c, b, and page 3 is empty.restartgoes back to the start, so the nextnextis page 1: [d, c].
Example 2
- Input:
- [3,["post x","post y","post z","post x","delete q","page 1","delete z","page 1","page 2","next"]]
- Output:
- [["x","z","y"],["x","y"],[],[]]
- Explanation
- Posting x again moves it back to the top, so the feed is x, z, y. Deleting q does nothing.
page 1shows all three; after z is deletedpage 1shows [x, y] andpage 2is empty. Thenextthat follows continues after page 2, so it is page 3, which is empty too.
Example 3
- Input:
- [1,["next","post a","post b","page 2","next","next"]]
- Output:
- [[],["a"],[],[]]
- Explanation
- On an empty feed the first
nextis page 1 and returns []. With page size 1 and the feed b, a,page 2returns [a]; the following twonextcalls are pages 3 and 4, both empty, because pages never wrap around.
Constraints
1 <= page_size <= 500 <= ops.length <= 500- Each op is
"post ID","delete ID","page K","next"or"restart", where an ID is 1 to 6 lowercase letters or digits and1 <= K <= 1000