Uber | Screening question

Anonymous User
1437
Feb 03, 2023
Feb 03, 2023

Given a stream of events . Each event has 3 parameters {driverId, timestamp at which event happened , type of event} . Search the event/events that happened at (or just before) the given timestamp and by the given driver id .

Eg : Input Events
{1, 1250 , PICKUP}
{1, 1252, PICKUP}
{1, 1252, DROP}
{1, 2582 , DROP}
{3, 4590 , PICKUP}

  1. On calling method Lookup (driverid -1 , timestamp - 1253)
    Output -> {1,1252, DROP}

  2. On calling method Lookup (driverid -1 , timestamp - 1252)
    Output -> {1, 1252, PICKUP}
    {1, 1252, DROP}

==========================================================

Solution that I gave is using HashMap , where
Key -> driverID
Value -> TreeSet having timestamp,typeof event . Events will be sorted in the ascending order of timestamp.

While doing lookup , we need to search for the floor value as per the timestamp for that DriverId in th treeset.

Only problem here is if more than one events are the part of output , we might need to apply BST seperately as lower() or higher() just gives one value.

Please share your thoughts and the alternative approaches to solve it.

Comments (7)