← 返回 snapchat 的题目列表Prefix Key-Value Sum Query (MapSum)
类型:online_judge
Problem
You maintain a key-value store with pairs (key, value) where:
key is a string
value is an integer
Support two operations:
insert(key, value): insert or update key with the given value.
sum(prefix): return the sum of values of all keys that start with prefix.
Example
Data:
(apple, 100)
(app, 20)
Query: sum("app")
Return: 120.
Constraints (interview-friendly)
1 <= key.length <= 50
keys contain only lowercase a-z
value fits in 32-bit signed int
total operations Q up to 2 * 10^5
Design a data structure to support the operations efficiently.
Example
Input
insert apple 100
insert app 20
sum app
Output
120