Validate a Shuffled Start/Stop Service Log for Each Component
Arrays & HashingAuto-graded · 26 test casesLast updated
Replay timestamped start/stop events in time order, with ties broken by input position, tracking one active job per category, and confirm the log is valid and fully closed.
A workshop records service work on a machine as a log of events stored in three parallel lists. For event i:
actions[i]is either"open"(a job of that category begins) or"end"(the currently running job of that category finishes).times[i]is the moment the event occurred.types[i]is the category of the job, for example"oil"or"brake".
The machine is cleared for departure only if the whole log is consistent and nothing is left running once every event has been replayed. Return True if the machine is cleared and False otherwise.
Replay rules
- The log may be shuffled. Replay events in ascending order of
times[i]. When two events share the same time, the one with the smaller index in the input goes first. - A category can have at most one running job at any moment. An
"open"event is invalid if a job of the same category is already running. - An
"end"event must finish a running job of its own category. It is invalid if no job of that category is running at that point. - After a job of a category has finished, a later
"open"for that category is valid again. - Categories are compared as exact strings and never influence one another, so several different categories may run simultaneously.
- If any event is invalid, the result is
False. Otherwise the result isTrueexactly when no job is still running after the final event. - An empty log yields
True.
Function
Implement is_ready_to_go(actions, times, types) returning a boolean, with the parameters in the order given above.
Constraints
0 <= len(actions) == len(times) == len(types) <= 100000actions[i]is exactly"open"or"end".0 <= times[i] <= 10^9types[i]consists of 1 to 20 lowercase English letters.- The result is uniquely determined by the input.
Examples
Example 1
Input: is_ready_to_go(['open', 'open', 'end', 'end'], [1, 2, 3, 4], ['oil', 'brake', 'brake', 'oil'])
Output: True
Example 2
Input: is_ready_to_go(['open', 'open', 'end', 'end'], [1, 2, 3, 4], ['oil', 'oil', 'oil', 'oil'])
Output: False
Example 3
Input: is_ready_to_go(['end', 'open', 'end', 'open'], [9, 1, 3, 6], ['tire', 'tire', 'tire', 'tire'])
Output: True
Hints
Hint 1
Before checking anything, decide the processing order. Sorting indices by the pair (time, original index) gives exactly the order the rules require.
Hint 2
Since each category has at most one running job, all you need to remember per category is a single yes/no: is it running right now? A set of running categories is enough.
Hint 3
Counting opens versus ends, or using one global stack, is not sufficient. Check two failure cases during replay (open while running, end while not running) and one more after the replay (anything still running).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each category behaves like an independent switch with two states: idle or running.
- Sort the event indices by the key
(times[i], i). This is precisely the replay order from the statement: earlier time first, and for equal times the smaller input index first. - Walk through the events in that order while maintaining a set of categories that are currently running.
- On
"open"for categoryt: iftis already in the set, the log is invalid, so returnFalse. Otherwise addt. - On
"end"for categoryt: iftis not in the set, there is nothing to finish, so returnFalse. Otherwise removet.
- On
- After the last event, return
Trueonly if the set is empty.
Why it is correct
For a single category, the rules form a two-state machine. From idle, open leads to running and end is invalid. From running, end leads to idle and open is invalid. Categories do not interact, so the state of the whole system is simply the set of running categories, which is what the algorithm maintains. Each branch corresponds to one transition and each early return False to one invalid transition. Because events are replayed in the mandated order, the first invalid event found is the right one, and since any invalid event forces False, stopping early is safe. If no event is invalid, the only remaining requirement is that nothing is still running, which is not running. An empty log skips the loop and yields True.
Common shortcuts fail: comparing counts of opens and ends accepts open, open, end, end on one category; a single global stack wrongly rejects open oil, open brake, end oil, end brake; skipping the sort misreads a log whose first listed event is a late end.
Complexity
- Time: O(n log n) for the sort, plus O(n) set operations on short strings.
- Space: O(n) for the sorted index list and the set.
Reference solution
from typing import List
def is_ready_to_go(actions: List[str], times: List[int], types: List[str]) -> bool:
# Replay by time; equal times go to the smaller input index.
order = sorted(range(len(actions)), key=lambda i: (times[i], i))
running = set() # categories that currently have a running job
for i in order:
t = types[i]
if actions[i] == "open":
if t in running:
return False
running.add(t)
else:
if t not in running:
return False
running.remove(t)
return not running