Score Tasks From Event Log Strings
StringsAuto-graded · 21 test casesLast 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 pointRETRY→ 2 pointsCOMPLETED→ 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
IDis a non-empty alphanumeric string containing no colon. - Each
COMMANDmay be any string, but onlySTART,RETRY, andCOMPLETEDadd 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
One pass over the records is enough. Two small structures do the work:
totals— a dict from task ID to running point count. A task gets an entry the first time it appears, even if none of its commands score.finished— a set of task IDs that have reportedCOMPLETEDat least once.
For each record, split on the first colon only (split(":", 1)) so that any colons inside the command are preserved. Then look the command up in a points table (START → 1, RETRY → 2, COMPLETED → 5); if present, add to that task's total. If the command is COMPLETED, also record the task in finished. Commands that are not in the table are simply ignored, which implements the "everything else scores zero" rule without an explicit branch.
After the loop, build the result by keeping only tasks that are in finished. Since totals was filled in first-appearance order, the output inherits that order.
Why It Is Correct
- Grouping: each record touches only the entry for its own task ID, so interleaved or out-of-order records accumulate correctly.
- Scoring: addition is commutative, so summing points as records arrive is identical to grouping first and summing later. Repeated
COMPLETEDrecords each add 5, as required. - Eligibility: since
COMPLETEDmight be the final record for a task, the decision is deferred until the whole input is read.finishedthen contains exactly the tasks that completed at least once. - Splitting: IDs are alphanumeric with no colon, so the first colon is always the delimiter, and
maxsplit=1keeps the rest of the command verbatim.
Complexity
Let N be the number of records, K the number of distinct tasks, and L the average record length.
- Time: O(N · L) because each record is split once and hashed in expected O(1); the final filtering costs O(K). With short strings this is effectively O(N).
- Space: O(K) for
totals,finished, and the output, plus O(L) per split. No per-command lists are stored.
from typing import Dict, List
def score_task_logs(events: List[str]) -> Dict[str, int]:
points = {"START": 1, "RETRY": 2, "COMPLETED": 5}
totals: Dict[str, int] = {}
finished = set()
for record in events:
task_id, command = record.split(":", 1)
totals.setdefault(task_id, 0)
if command in points:
totals[task_id] += points[command]
if command == "COMPLETED":
finished.add(task_id)
return {task_id: totals[task_id] for task_id in totals if task_id in finished}