Redis's LIST type is a doubly linked list under the hood, which is what makes push and pop at both ends O(1). You will build a small version of it.
Implement the RedisList class backed by a doubly linked list (a Python list/deque or C++ vector/deque defeats the purpose — the point is to manage the nodes yourself):
lpush(self, values: list) -> int — push each value onto the head (left), one at a time in the given order, and return the new length. lpush(["a", "b", "c"]) leaves the list as c, b, a.rpush(self, values: list) -> int — push each value onto the tail (right), one at a time, and return the new length.lpop(self, count: int = 1) -> list — remove up to count values from the head and return them in pop order. If the list has fewer than count elements, return everything. On an empty list return [].rpop(self, count: int = 1) -> list — same, from the tail.All four operations must be O(1) per element (O(count) for a pop of count items).
Example:
lpush(["a", "b", "c"])→ 3 (list is nowc, b, a)rpush(["d", "e"])→ 5 (list is nowc, b, a, d, e)lpop(2)→["c", "b"](list is nowa, d, e)rpop(1)→["e"](list is nowa, d)
Constraints:
count >= 1Official solution locked
Give the problem a real attempt before peeking — revealing it affects your credit for this problem for 4 hours.