KatInterview

Validate a Shuffled Start/Stop Service Log for Each Component

Arrays & HashingAuto-graded · 26 test cases
MediumAuto-gradedArrays & Hashinghash-setsortingsimulation

Last 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 is True exactly 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) <= 100000
  • actions[i] is exactly "open" or "end".
  • 0 <= times[i] <= 10^9
  • types[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).