KatInterview

Replay Keyed Updates and Report Per-Group Peak Values

Data Structure DesignAuto-graded · 19 test cases
EasyAuto-gradedData Structure Designhash-mapsimulationrunning-maximum

Last updated

Simulate a log of keyed insert and query commands, keeping a running maximum for each key so single-key and all-key peak queries are answered without rescanning data.

Replay Keyed Updates and Report Per-Group Peak Values

You are given a recorded sequence of commands sent to an in-memory tracker. The tracker sorts numeric readings into named groups and remembers the largest reading seen so far in each group. Your job is to replay the commands in order and return what each command produces.

You receive two lists of equal length:

  • commands: a list of strings, the command names.
  • payloads: a list of lists, where payloads[i] holds the arguments of commands[i].

Every group is identified by a string key. A group does not exist until its first reading arrives.

Commands

CommandPayloadEffectOutput
"add"[key, value]Record the numeric value in the group named key, creating the group if it is new. The group's peak becomes the larger of its old peak and value.None
"max"[key]Look up the current peak of the group named key.The peak, or None if no reading was ever added to that key.
"all_maxima"[]Report every existing group together with its current peak.A list of [key, peak] pairs sorted in ascending order by key (standard string comparison). The list is empty if no groups exist.

Notes

  • Values can be negative, zero, or positive. A group whose only readings are negative has a negative peak.
  • Repeated or equal values never change a peak in a harmful way: the peak is simply the largest value seen.
  • A "max" query for an unknown key does not create the group.
  • Any command name other than the three above is invalid; raise a ValueError for it.

Task

Return a list with one entry per command, in the same order as commands, holding the output of each command as described in the table.

Constraints

  • len(commands) == len(payloads)
  • Keys are strings; values are numbers (integers in the tests).
  • Each "add" and "max" command should run in expected O(1) time, and "all_maxima" in O(g log g) time where g is the number of existing groups.
  • Do not rescan earlier readings to answer a query.

Examples

Example 1

Input: replay_group_peaks(['add', 'add', 'max', 'add', 'max', 'all_maxima'], [['a', 5], ['a', -2], ['a'], ['b', 7], ['b'], []])

Output: [None, None, 5, None, 7, [['a', 5], ['b', 7]]]

Example 2

Input: replay_group_peaks(['max', 'add', 'max', 'add', 'max'], [['missing'], ['x', -10], ['x'], ['x', -3], ['x']])

Output: [None, None, -10, None, -3]

Example 3

Input: replay_group_peaks(['add', 'add', 'add', 'all_maxima'], [['z', 4], ['a', 4], ['z', 4], []])

Output: [None, None, None, [['a', 4], ['z', 4]]]

Example 4

Input: replay_group_peaks(['add', 'max', 'all_maxima'], [['cluster-1', 0], ['cluster-1'], []])

Output: [None, 0, [['cluster-1', 0]]]

Hints

Hint 1

You never need to remember individual readings, only the largest one per key. What container maps a key to a single number?

Hint 2

On an add, compare the new value with the stored peak only if the key already exists; otherwise the new value becomes the peak. Be careful not to treat a peak of 0 or a negative peak as 'missing'.

Hint 3

For a max query use a lookup that returns None for missing keys without inserting anything. For all_maxima, sort the keys and build [key, peak] pairs fresh each time.