Search⌘ K
AI Features

Map Sum Pairs

Explore how to implement a MapSum data structure that inserts key-value pairs and returns the sum of values for keys sharing a prefix. Understand using trie patterns to handle prefix queries efficiently and update values dynamically, enhancing your problem-solving skills for coding interviews.

Statement

Design a data structure that supports the following operations:

  1. Insert a key-value pair:

    1. Each key is a string, and each value is an integer.

    2. If the key already exists, update its value to (overriding the previous value).

  2. Return the prefix sum:

    1. Given a string, prefix, return the total sum of all values associated with keys that start with this prefix.

To accomplish this, implement a class MapSum:

  • Constructor: Initializes the object.

  • void insert (String key, int val): ...