Implement a caching reader for remote file access. A RemoteReader can read bytes from a server by offset and size. Your task is to implement CachingReader so that repeated reads minimize calls to the remote reader by caching data locally.
Then solve a second question: given a string containing only brackets such as { and }, implement a function isBalanced that returns a boolean indicating whether the string is balanced.
Question 1: Caching Reader
class RemoteReader:
def read(self, offset: int, size: int) -> bytes:
# already implemented, you can call this method
class CachingReader:
# Maximum memory used by a caching reader
# MaxCacheSize: int = 10000
def read(self, offset: int, size: int) -> bytes:
# TODO: implement this method
def __init__(self):
# TODO: feel free to add anything you like
self.__reader = RemoteReader()
Question 2: Balanced Brackets
Input: String containing { and }
- Balanced → true
- Non-balanced → false
Write a function isBalanced(str: String) -> Boolean.
This set of questions combines two classic interview patterns. The first asks you to build a caching layer on top of a remote byte reader, where the goal is to reduce repeated network calls by storing previously fetched data locally and serving overlapping requests from cache whenever possible. The second is a balanced-brackets check for strings containing only <code>{</code> and <code>}</code>, which can be solved with a simple counter or stack to verify that every prefix is valid and the final state is balanced.