Google Phone screen L3/ L4
Anonymous User
1465

You have a series of log entries indicating the start and finish times of various requests. Each log entry consists of a time and a request ID. For instance:

Start(1, req1)
Finish(2, req1)
Start(3, req2)
Start(4, req3)
Start(5, req4)
Finish(6, req4)
Finish(7, req3)
Finish(8, req2)

Your task is to process these log entries and produce an output string that indicates the start and end times for each request. The output should be sorted by the finish times of the requests. The format should be:

Req1 started at 1 and ended at 2
Req4 started at 5 and ended at 6
Req3 started at 4 and ended at 7
Req2 started at 3 and ended at 8

My solution was to create interface with start(), Finish() and printlogs() method. I used two stacks to solve the problem.

my solution

I was putting a pair (long, string) into a stack whenever I encountered a start log entry. When I saw a finish log entry, I matched it with the values already present in the stack. In the worst-case scenario, this matching could take O(n) time, but it was still a good approach. Initially, I proposed a solution using a hashmap and heap, but I discarded it due to the real-time nature of the data, which could cause the hashmap to grow large and increase time complexity. I used two stacks so that while matching the finished request, I could temporarily hold some values and later push them back.

Comments (14)