We have stream of incoming messages, each associated with timestamp. Print the unique messages within 10 seconds interval.
Similar to https://leetcode.com/problems/logger-rate-limiter/
Question was open ended and I had to ask many questions to frame it well. Came up with my own test cases, method definitions, assumptions and clarified it with interviewer.
Follow up (Tricky): Ignore any messages that are duplicated within 10 seconds interval.
We have an array of nums with duplicates. Return true if there exists any duplicate number within d distance.
arr = [1,2,3,4,5,2] d=4
o/p = true;
In the above example, there is a duplicate present which is number 2 at index 1 and index 5. Distance b/w two numbers is the absolute difference between their indexes. In this case, it is 4 and which is matching the given criteria (i.e. d=4).
So our method should return true when the distance between duplicates is within the given criteria so following test cases will hold true as well,
arr = [1,2,3,4,2,5] d=4
o/p = true; //distance b/w 2s is 3
arr = [1,2,3,5,2,5] d=4
o/p = true; //distance b/w 5s is 2 and 2s is 3
Note: We can return true after encountering one pair of duplicates meeting our criteria
arr = [1,2,3,4,5,2] d=2
o/p = false; //distance b/w 2 is 4 but our criteria is 2 so return false