KatInterview

Score Tasks From Event Log Strings

StringsAuto-graded · 21 test cases
MediumAuto-gradedStringsstringshash-mapsimulation

Last updated

Parse "id:command" log lines, accumulate weighted scores per task with a hash map, and keep only tasks that completed.

Problem Statement

A distributed job runner emits a stream of event records. Each record is a string of the form "ID:COMMAND", where ID is the identifier of a task and COMMAND is the action that was reported for it.

Write a function that walks the records in order and produces a dictionary mapping each task ID to its accumulated execution score — but only for tasks that reported the command COMPLETED at least once. Tasks that never completed are omitted entirely.

Points are awarded as follows:

  • START → 1 point
  • RETRY → 2 points
  • COMPLETED → 5 points
  • Any other command → 0 points, and it does not affect eligibility

Every occurrence of a scoring command counts, so a task may contain several COMPLETED records and each contributes 5.

Commands match exactly and are case-sensitive: "completed" is not the same as "COMPLETED". The ID is separated from the command by the first colon only; if the command portion itself contains a colon (e.g. "job7:ERR:timeout"), everything after the first colon is the command.

Return a dict[str, int]. Keys should appear in the order each task ID is first encountered (standard Python dict insertion order).

Function Signature

def score_task_logs(events: list[str]) -> dict[str, int]:

Constraints

  • 0 <= len(events) <= 10_000
  • Each ID is a non-empty alphanumeric string containing no colon.
  • Each COMMAND may be any string, but only START, RETRY, and COMPLETED add points.
  • Input is assumed well formed: each record contains at least one colon.

Notes

  • An empty input list returns an empty dictionary.
  • A task that appears only with unrecognized commands is excluded, since it never completed.
  • Records for different tasks may be interleaved in any order.

Examples

Example 1

Input: score_task_logs(['task1:START', 'task1:RETRY', 'task1:COMPLETED', 'task2:START', 'task2:PENDING'])

Output: {'task1': 8}

Example 2

Input: score_task_logs(['a:COMPLETED'])

Output: {'a': 5}

Example 3

Input: score_task_logs(['x:START', 'y:RETRY', 'x:PENDING', 'y:COMPLETED', 'x:COMPLETED'])

Output: {'x': 6, 'y': 7}

Hints

Hint 1

Split each record on the first colon only, so a command that itself contains a colon stays intact.

Hint 2

Keep a running total per task and a separate marker for whether the task ever completed; decide what to return at the very end.

Hint 3

A dictionary from command to points makes 'everything else scores zero' fall out automatically.