This question evaluates the ability to maintain and query aggregated state with frequent updates, focusing on data structures for efficient real-time tracking of maximum values and dynamic key-value aggregations in the coding and algorithms domain.
You are building a core component for a real-time analytics system that tracks the profitability of experiences on a large online platform.
Implement a function that processes a sequence of operations and returns the result of each query.
You are given three arrays of equal length:
operations[i]
: either
'U'
for an update or
'Q'
for a query
experiences[i]
: the name of the experience involved in operation
i
deltas[i]
: the profit change for the experience if
operations[i] == 'U'
; otherwise this value is ignored
Rules:
'U'
, add
deltas[i]
to the current total earnings of
experiences[i]
'Q'
, return the name of the experience with the highest total earnings at that moment
0
before applying its first update
Return an array containing the answers for all query operations, in order.
Example:
operations = ['U', 'U', 'Q', 'U', 'Q']
experiences = ['GameA', 'GameB', '', 'GameA', '']
deltas = [5, 7, 0, 4, 0]
Processing:
GameA
by
+5
→ totals:
GameA = 5
GameB
by
+7
→ totals:
GameB = 7
GameB
GameA
by
+4
→ totals:
GameA = 9
GameA
Output: ['GameB', 'GameA']
Design an efficient solution for processing many updates and queries.