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.
这道题包含两个经典面试方向:第一个是实现带缓存的远程读取器,需要在本地保存已经读过的数据,尽量减少对远端 <code>RemoteReader</code> 的重复访问,核心思路通常是用区间缓存或按块缓存来覆盖请求范围;第二个是括号匹配,输入只包含 <code>{</code> 和 <code>}</code> 时,可以用计数器或栈来判断是否始终保持前缀合法,最终计数归零即为平衡。第一个问题更强调缓存命中、区间合并和内存上限控制,第二个问题则考察基础的栈 / 计数判断能力。