Build a simple endpoint that can filter by a time range, user, and currency.
Some example use cases here would be to display some sort of transaction history. For simplicity, let’s just build this using a typed interface in your language of choice, but we can pretend there is a service boundary.
Let’s also use this sample dataset so we can build an endpoint we can use, but you can imagine in a real situation we would have a database. We do not want to build a database here, so it is okay to loop through the items, but do consider what you are doing that might be different in an actual database-backed implementation.
[{"time": 1, "id": 11, "user_id": 1, "currency": 1, "amount": 10},
{"time": 2, "id": 12, "user_id": 1, "currency": 3, "amount": 11},
{"time": 3, "id": 13, "user_id": 2, "currency": 1, "amount": -10},
{"time": 4, "id": 14, "user_id": 1, "currency": 2, "amount": 12},
{"time": 5, "id": 5, "user_id": 1, "currency": 2, "amount": -10},
{"time": 6, "id": 6, "user_id": 1, "currency": 1, "amount": 13},
{"time": 7, "id": 7, "user_id": 1, "currency": 2, "amount": -5},
{"time": 8, "id": 8, "user_id": 1, "currency": 2, "amount": 10},
{"time": 9, "id": 9, "user_id": 1, "currency": 1, "amount": 10},
{"time": 10, "id": 10, "user_id": 1, "currency": 1, "amount": 10},
{"time": 10, "id": 21, "user_id": 1, "currency": 1, "amount": 16},
{"time": 11, "id": 22, "user_id": 1, "currency": 2, "amount": -3},
{"time": 11, "id": 23, "user_id": 1, "currency": 1, "amount": 5},
{"time": 12, "id": 24, "user_id": 1, "currency": 2, "amount": 6},
{"time": 13, "id": 25, "user_id": 1, "currency": 1, "amount": 10}
]
这题要求你实现一个简单的查询接口:根据时间范围、用户 ID 和币种筛选交易记录。核心思路通常是先定义清晰的数据结构和请求参数,再对样本数据逐条过滤,保留满足条件的记录并返回结果。由于题目明确说明不需要真实数据库,直接遍历数组即可;但在工程实现上,也要意识到如果接到数据库,过滤条件最好下推到查询层,并注意时间边界、空条件以及多条件组合筛选的处理方式。