Uber | Onsite | Stream Data
Company: Uber
Role: unknown
Interview Stage: Onsite
Location: unknown
Year: unknown
Original Post Content:
You are getting stream of data which is stored by calling the function NewData.
void NewData(int time, String key, int value) {
// code which takes the input and stores in some data structure
}
And then you call top3 which returns the top 3 keys in the last 10 units of time.
List<String, Integer> top3(int t) {
// returns the top 3 keys from time 't-9' to time 't' inclusive
}
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)]