FunctionRacerProblemsRace

Self Time per Function

MediumStacks and queuesPython, JavaScript, TypeScript, Java, C++

logs records a single-threaded program in time order. Each entry is "NAME start T" or "NAME end T", where T is a moment in time. A call runs from its start to its end, but it is paused while a call that started inside it is running. Calls are properly nested: each end closes the most recent call that has not ended, which has the same name. A function may call itself.

The self time of a call is the time it was running and not paused, so a call from moment 2 to moment 5 with nothing inside it has self time 3. Return a dictionary mapping each function name to the total self time of all its calls (an empty dictionary when logs is empty).

Example 1

logs ["main start 0","load start 2","load end 5","main end 9"]
returns {"main":6,"load":3}

main runs from 0 to 2 and from 5 to 9, which is 6, and load runs from 2 to 5.

Example 2

logs ["f start 0","f start 1","g start 3","g end 3","f end 4","f end 6"]
returns {"f":6,"g":0}

Between them, the two calls of f run from 0 to 1, 1 to 3, 3 to 4 and 4 to 6, which is 6. The call of g takes no time.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems