KatInterview

Risk-Ranked Alert Queue Simulator

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designpriority-queueheapdesign

Last updated

Simulate a priority queue of alerts that serves the highest risk first and the oldest first on ties, driven by a list of Put and GetNext commands.

You are building the core of an alert-triage queue. Each alert has three string fields: an identifier, a risk score, and a payload. Alerts are fed in one at a time, and whenever the analyst asks for work, the queue hands out the most urgent alert and removes it.

Rather than building a class, you will simulate the queue. You are given a list of commands and a parallel list of their arguments. Process the commands in order and return one result per command.

Commands

  • "Put": the arguments are [alert_id, risk_score, payload]. Add the alert to the queue. The result for this command is None.
  • "GetNext": the arguments are an empty list. Remove the most urgent alert from the queue and return it as [alert_id, risk_score, payload], with all three values as strings. If the queue is empty, the result is None and nothing changes.

Ordering rules

  • The alert with the highest risk score is served first.
  • Among alerts with the same risk score, the one that was added earliest (by its Put order) is served first.
  • Risk scores are non-negative integers written as decimal strings. Compare them as numbers, not as text. They can be as large as an unsigned 64-bit integer (up to 18446744073709551615).
  • When an alert is returned, its score is written in canonical decimal form, so a score given as "007" is returned as "7".

Duplicate identifiers

Identifiers are not checked for uniqueness. If Put is called with an alert_id that is already in the queue, it is simply a separate alert. Nothing is replaced, merged, or rejected, and each copy is served according to its own score and arrival order.

Input and output

  • commands[i] is either "Put" or "GetNext", and arguments[i] holds the arguments for that command.
  • Return a list of the same length as commands. Entry i is None for every Put and for a GetNext on an empty queue, and otherwise a list of three strings.

Constraints

  • len(commands) == len(arguments).
  • Every command is either "Put" or "GetNext".
  • For "Put", arguments[i] has exactly three strings. The second is a non-negative integer in decimal form, at most 2^64 - 1.
  • For "GetNext", arguments[i] is an empty list.
  • Aim for O(log n) time per command, where n is the number of alerts currently queued.

Examples

Example 1

Input: risk_queue_simulation(['Put', 'Put', 'GetNext', 'GetNext', 'GetNext'], [['det-1', '5', 'alert'], ['det-2', '9', 'malware'], [], [], []])

Output: [None, None, ['det-2', '9', 'malware'], ['det-1', '5', 'alert'], None]

Example 2

Input: risk_queue_simulation(['Put', 'Put', 'GetNext', 'GetNext'], [['low', '2', 'a'], ['high', '7', 'b'], [], []])

Output: [None, None, ['high', '7', 'b'], ['low', '2', 'a']]

Hints

Hint 1

Which standard data structure gives you the best element in logarithmic time while still allowing insertions?

Hint 2

Python's heapq is a min-heap. Think about how to make the highest score come out first, and how to make the earliest insertion win among equal scores.

Hint 3

Convert the score to an int before comparing, so ordering is numeric. Add an increasing counter to each entry as a tie-breaker, so you never compare ids or payloads.