注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
求大米求大米!想看面经
问题如下:
# You are asked to design a backend in-memory database for a thermostat device. The data sent by the device has a certain pattern: [timestamp(XXXX-XX-XX XX:XX:XX), temperature(e.g 24.6)] and the database is supposed to answer the following two questions effectively:
# 1. What is the temperature at <t1>?
# 2. Give me all the temperature data from <t1> to <t2>
# data stream:
# 2023-01-01 10:01:01 24.6
# 2023-01-01 10:02:02 24.7
# 2023-01-01 11:02:00 27.6
# query:
# Give me all the temperature data from 2023-01-01 10:00:00 to 2023-01-01 11:00:00
# 2023-01-01 10:01:01 24.6
# 2023-01-01 10:02:02 24.7
# Ask:
# 1. Define the interfaces of this database (read & write operations)
# 2. Provide the implementation
# 3. Validate the correctness with test cases
coding不难,10分钟写完,对应leetcode估计easy-medium,但是深挖了半个多小时的八股。
问的问题在我的回忆里包括不仅限于:- 二分查找速度不够ect_left(self.records, (end, float('inf')))
return self.records[start_index: end_index]
db = Database()
db.insert("2023-01-01 10:01:01", 24.6)
db.insert("2023-01-01 10:02:02", 24.7)
db.insert("2023-01-01 11:02:00", 27.6)
print(db.get_temp_at("2023-01-01 10:01:01"))
print(db.get_temp_in_range("2023-01-01 10:00:00", "2023-01-01 11:00:00"))
# 2023-01-01 10:01:01 24.6
# 2023-01-01 10:02:02 24.7
# 2023-01-01 11:02:00 27.6
|