BackhardTrieGoogleAmazon

Vault Interval Evaluator 19 Solution

Problem Statement

You are tasked with implementing a high-performance data structure to manage a dynamic set of alphanumeric keys representing vault access tokens. The system must support two primary operations: inserting a new token and querying the total weight of all tokens that share a specific prefix. Each token is associated with a unique integer weight. The core challenge lies in efficiently aggregating these weights during prefix queries without traversing the entire dataset, leveraging the hierarchical nature of the Trie structure.

Specifically, you must design a class VaultIntervalEvaluator that initializes an empty Trie. It should provide a method insert(key: str, weight: int) to add a key with its corresponding weight. If the key already exists, the weight should be updated. Additionally, implement a method prefixSum(prefix: str) that returns the sum of weights for all keys in the Trie that start with the given prefix. If no such keys exist, return 0. The solution must handle large datasets efficiently, ensuring that both insertion and query operations run in O(L) time complexity, where L is the length of the key or prefix.

Example 1
Input
evaluator = VaultIntervalEvaluator() evaluator.insert("vault1", 10) evaluator.insert("vault2", 20) evaluator.insert("vault10", 30) print(evaluator.prefixSum("vault1"))
Output
40

Explanation: 1. Insert 'vault1' with weight 10. The Trie stores this path with a cumulative weight of 10 at the terminal node. 2. Insert 'vault2' with weight 20. This is a distinct path. 3. Insert 'vault10' with weight 30. This extends the 'vault1' path. The node for 'vault1' now has a subtree containing 'vault1' (10) and 'vault10' (30). 4. Query prefixSum("vault1"). The algorithm traverses to the node corresponding to 'vault1'. It sums the weights of all keys in the subtree rooted at this node. The keys are 'vault1' (10) and 'vault10' (30). Total = 10 + 30 = 40.

Example 2
Input
evaluator = VaultIntervalEvaluator() evaluator.insert("alpha", 5) evaluator.insert("alphabet", 15) evaluator.insert("beta", 25) print(evaluator.prefixSum("alph"))
Output
20

Explanation: 1. Insert 'alpha' (5) and 'alphabet' (15). Both share the prefix 'alph'. 2. Insert 'beta' (25). This is unrelated to the 'alph' prefix. 3. Query prefixSum("alph"). The algorithm navigates to the node for 'alph'. The subtree contains 'alpha' and 'alphabet'. 4. Sum of weights = 5 + 15 = 20.

Example 3
Input
evaluator = VaultIntervalEvaluator() evaluator.insert("key", 100) print(evaluator.prefixSum("key")) print(evaluator.prefixSum("k")) print(evaluator.prefixSum("xyz"))
Output
100 100 0

Explanation: 1. Insert 'key' with weight 100. 2. Query prefixSum("key"): Matches the exact key. Sum = 100. 3. Query prefixSum("k"): 'key' starts with 'k'. Sum = 100. 4. Query prefixSum("xyz"): No keys start with 'xyz'. Sum = 0.

Constraints

  • 1 <= key.length <= 100
  • key and prefix consist of lowercase English letters only.
  • 1 <= weight <= 10^9
  • At most 10^5 calls will be made to insert and prefixSum.
  • The total number of characters in all keys inserted will not exceed 10^6.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Vault Interval Evaluator 19 — Problem Statement & Solution Guide

TrieHardRecursive Backtracking
TimeO(L) per insert or query
|
SpaceO(T * L) where T is number of distinct tokens

Problem Description

You are tasked with implementing a high-performance data structure to manage a dynamic set of alphanumeric keys representing vault access tokens. The system must support two primary operations: inserting a new token and querying the total weight of all tokens that share a specific prefix. Each token is associated with a unique integer weight. The core challenge lies in efficiently aggregating these weights during prefix queries without traversing the entire dataset, leveraging the hierarchical nature of the Trie structure.

Specifically, you must design a class VaultIntervalEvaluator that initializes an empty Trie. It should provide a method insert(key: str, weight: int) to add a key with its corresponding weight. If the key already exists, the weight should be updated. Additionally, implement a method prefixSum(prefix: str) that returns the sum of weights for all keys in the Trie that start with the given prefix. If no such keys exist, return 0. The solution must handle large datasets efficiently, ensuring that both insertion and query operations run in O(L) time complexity, where L is the length of the key or prefix.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Vault Interval Evaluator 19"

hard

WHY DOES IT MATTER?

Prefix‑based aggregation is a core operation in many search and analytics systems.

OPTIMIZATION CHALLENGE

Transforming an O(N) scan into O(L) traversal cuts runtime dramatically for large datasets.

REAL-WORLD CONNECTION

Search engines index terms and quickly compute document frequencies for query prefixes.

Cache the cumulative sum at each node and update it incrementally during inserts/deletes to avoid recomputation.

COMPLEXITY AT A GLANCE

⏱ Time:O(L) per insert or query
đź’ľ Space:O(T * L) where T is number of distinct tokens

Core Theory — Why This Approach?

A Trie (prefix tree) stores characters of keys along a path from the root, allowing aggregation of values at each node. By maintaining a cumulative weight at every node, a prefix query becomes a simple traversal to the node representing the prefix, returning its stored sum in O(L) time, where L is the prefix length.

Naïve solutions such as scanning all tokens or using a hash map of full keys require O(N) per query, which is prohibitive when N and Q reach 10^5 or more. The optimal paradigm leverages the hierarchical nature of strings: inserting updates O(L) nodes, and prefix sums are pre‑computed during insertion, yielding logarithmic‑ish performance independent of the total number of tokens.

Interview Questions on This Problem

Q1How does a Trie enable O(L) prefix sum queries compared to a hash map?

Each node aggregates the weights of all strings passing through it, so reaching the node for a prefix directly yields the total. A hash map lacks this hierarchical aggregation and would need to examine every key.

Q2What modifications are needed to support deletion of a token while keeping prefix sums correct?

During deletion, traverse the token’s path and subtract its weight from each node’s cumulative sum. Optionally prune nodes whose sum becomes zero to save space.

Q3Why is it safe to store cumulative sums as 64‑bit integers in this problem?

The weight constraints and number of tokens can cause the total sum to exceed 32‑bit range. Using 64‑bit (long long) prevents overflow while keeping arithmetic O(1).

Examples

Example 1

Input

evaluator = VaultIntervalEvaluator()
evaluator.insert("vault1", 10)
evaluator.insert("vault2", 20)
evaluator.insert("vault10", 30)
print(evaluator.prefixSum("vault1"))

Output

40

Explanation: 1. Insert 'vault1' with weight 10. The Trie stores this path with a cumulative weight of 10 at the terminal node. 2. Insert 'vault2' with weight 20. This is a distinct path. 3. Insert 'vault10' with weight 30. This extends the 'vault1' path. The node for 'vault1' now has a subtree containing 'vault1' (10) and 'vault10' (30). 4. Query prefixSum("vault1"). The algorithm traverses to the node corresponding to 'vault1'. It sums the weights of all keys in the subtree rooted at this node. The keys are 'vault1' (10) and 'vault10' (30). Total = 10 + 30 = 40.

Example 2

Input

evaluator = VaultIntervalEvaluator()
evaluator.insert("alpha", 5)
evaluator.insert("alphabet", 15)
evaluator.insert("beta", 25)
print(evaluator.prefixSum("alph"))

Output

20

Explanation: 1. Insert 'alpha' (5) and 'alphabet' (15). Both share the prefix 'alph'. 2. Insert 'beta' (25). This is unrelated to the 'alph' prefix. 3. Query prefixSum("alph"). The algorithm navigates to the node for 'alph'. The subtree contains 'alpha' and 'alphabet'. 4. Sum of weights = 5 + 15 = 20.

Example 3

Input

evaluator = VaultIntervalEvaluator()
evaluator.insert("key", 100)
print(evaluator.prefixSum("key"))
print(evaluator.prefixSum("k"))
print(evaluator.prefixSum("xyz"))

Output

100
100
0

Explanation: 1. Insert 'key' with weight 100. 2. Query prefixSum("key"): Matches the exact key. Sum = 100. 3. Query prefixSum("k"): 'key' starts with 'k'. Sum = 100. 4. Query prefixSum("xyz"): No keys start with 'xyz'. Sum = 0.

Constraints

  • 1 <= key.length <= 100
  • key and prefix consist of lowercase English letters only.
  • 1 <= weight <= 10^9
  • At most 10^5 calls will be made to insert and prefixSum.
  • The total number of characters in all keys inserted will not exceed 10^6.

Optimal Approach & Strategy

Build a Trie where each node keeps a cumulative weight; insert updates O(L) nodes and a prefix query reads the sum in O(L).

Brute Force Approach

Store all tokens in a list and, for each query, iterate over the list summing weights of tokens that start with the given prefix.

Verified Code Solutions

JavaScript Solution
Time: O(L) per insert or query
function solution(nums, K) {
   let sum = 0;
   if (nums.length === 0) return 0;
   for (let num of nums) {
       if (num > K) sum += num;
   }
   return sum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.