KatInterview

Priority Queue Scheduler + Expression Evaluator

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designheapparsingdesign

Last updated

Tests heap-based priority scheduling with tie-breaking and recursive-descent expression parsing with variable substitution.

You will implement two mini-systems inside a single function and return both results together.


Part 1: Job Queue with Priority and Timing

You are given a list of jobs that are pre-loaded into a queue at startup. Each job is described by three values: a string job_id, an integer priority, and an integer arrival_time.

After the initial load, a sequence of operations is applied one at a time:

  • "add_task" — insert a new job into the queue. The corresponding entry in operation_args is [job_id, priority, arrival_time].
  • "get_next_task" — remove and record the job that should run next. The corresponding entry in operation_args is [].

The job that runs next is always the one with the highest priority. When two jobs share the same priority, the one with the smaller arrival_time runs first. If two jobs share both priority and arrival time, the one that was inserted earlier (by insertion order across all additions, including the initial load) runs first.

Every "get_next_task" call appends its result to a list called processed. If the queue is empty when "get_next_task" is called, append None.


Part 2: Arithmetic Expression with Named Variables

You are also given a dictionary var_map mapping variable names to integer values, and a string expr containing an arithmetic expression.

Evaluate expr and return its integer result. The expression may contain:

  • Non-negative integer literals (possibly multi-digit).
  • Variable names made of letters, digits, and underscores, where the first character is a letter or underscore.
  • The operators + (addition) and * (multiplication), with standard precedence: * binds more tightly than +.
  • Parentheses () to override precedence.
  • Any amount of whitespace between tokens.

If any variable that appears in the expression is not present in var_map, return -1 for the expression value — even if the missing variable's contribution would evaluate to zero under ordinary arithmetic.


Return Value

Return a single dictionary with exactly two keys:

{
    "processed": <list of job_id strings and/or None values>,
    "expression_value": <integer>
}

Constraints

  • Number of pre-loaded jobs plus "add_task" operations: up to 10^5 total.
  • Number of operations: up to 10^5.
  • priority and arrival_time are non-negative integers.
  • Expression length: up to 10^4 characters.
  • Integer literals in the expression are non-negative.
  • Variable names contain only alphanumeric characters and underscores.
  • Operator precedence follows standard arithmetic: * before +; parentheses override.
  • Left-to-right associativity for operators of equal precedence.
  • Each "get_next_task" on an empty queue produces None (no error).

Examples

Example 1

Input: run_job_queue_and_eval([['T1', 10, 1], ['T2', 20, 2], ['T3', 10, 0]], ['get_next_task', 'get_next_task', 'get_next_task'], [[], [], []], {}, '0')

Output: {'processed': ['T2', 'T3', 'T1'], 'expression_value': 0}

Example 2

Input: run_job_queue_and_eval([], ['add_task', 'add_task', 'get_next_task', 'get_next_task'], [['A', 5, 10], ['B', 5, 3], [], []], {'x': 3}, 'x + 5 * 2')

Output: {'processed': ['B', 'A'], 'expression_value': 13}

Example 3

Input: run_job_queue_and_eval([['old', 1, 5]], ['get_next_task', 'get_next_task', 'add_task', 'get_next_task'], [[], [], ['new', 9, 8], []], {'a': 5, 'b': 10}, '(a + b) * 2')

Output: {'processed': ['old', None, 'new'], 'expression_value': 30}

Hints

Hint 1

For the job queue, Python's heapq is a min-heap. Think about how to negate or reorder the key tuple so that the 'best' job naturally ends up at the top of the heap.

Hint 2

A sequence counter incremented on every insertion gives you a stable third tie-breaker without ever comparing job_id strings — this also lets the heap avoid type errors when priorities and arrival times are equal.

Hint 3

For the expression evaluator, model the grammar in three layers — expression handles +, term handles *, and factor handles literals, variables, and parenthesised sub-expressions — and substitute variables as you parse rather than in a separate pass.