MapSum Pair Implementation

Create a data structure named 'MapSum' with the ability to perform two operations and a constructor.

Explanation:

Implement the following:
  • MapSum(): Initializes the MapSum.
  • insert(KEY, VAL): Inserts a key-value pair into the MapSum. If the key already exists, update its value.
  • sum(PREFIX): Calculates the sum of all values for which the keys have the specified prefix.

Input:

The first line will contain an integer 'T' representing the number of test cases.
For each test case, the first line contains an integer 'N', indicating the number of operations to perform.
Each of the next 'N' lines contains either a function call to 'insert' with a 'key-value' pair or 'sum' with a 'PREFIX'.

Output:

The output for each test case will be executed based on the function operations described above, printed on a separate line.
Example:

Suppose you have two test cases, and for the first test case:

N = 3
Operations:
insert("apple", 3)
sum("ap") -> outputs 3
insert("app", 2)
sum("ap") -> outputs 5

Constraints:

  • 1 <= T <= 50
  • 2 <= N <= 10000
  • 1 <= |KEY|, |PREFIX| <= 50
  • 1 <= VAL <= 1000

The |KEY| and |PREFIX| represent the lengths of the string key and prefix, while VAL denotes the value associated with a key.

Time limit: 1 second

Note:
It is not necessary to handle input or output explicitly; focus on implementing the required functions.
AnswerBot
1y

The question asks to implement a data structure called 'MapSum' with functions to initialize, insert key-value pairs, and find the sum of values with a given prefix.

  • Implement a class called 'MapSum' w...read more

Help your peers!
Add answer anonymously...
GlobalLogic Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter