Uber | Onsite | Stream Data

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2020-11-08Added By Anonymous
Source: Link

Company: Uber

Interview Stage: Onsite


Original Post Content:


You are getting stream of data which is stored by calling the function NewData.

1void NewData(int time, String key, int value) { 2 // code which takes the input and stores in some data structure 3}


And then you call top3 which returns the top 3 keys in the last 10 units of time.

1List<String, Integer> top3(int t) { 2 // returns the top 3 keys from time 't-9' to time 't' inclusive 3}


Implement these two functions. How will you store the data to optimize getting the data?

example ->


NewData(0, “a”, 1)

NewData(0, “a”, 2) => so after this, key ‘a’ has value 3 at time 0

NewData(0, 𠇋”, 5)

NewData(1, 𠇌”, 6)

NewData(2, 𠇍”, 3)

NewData(3, 𠇍”, 7)

NewData(4, 𠇍”, 1)

NewData(4, 𠇊”, 2)

NewData(5, 𠇋”, 4)

NewData(5, 𠇎”, 5)

NewData(5, 𠇎”, 6)

NewData(5, 𠇍”, 4)

NewData(6, 𠇊”, 2)

NewData(6, 𠇌”, 1)

.

.

.

.

NewData(13, 𠇌”, 5)

top3(13) => it should return aggregated top 3 keys from time 4 to time 13 like

[(‘d’, 12), (‘e’, 11), (‘c’, 6)]

Comments