I have a problem, input will contain loggedin user id and timestamp when user logged in. when user logs in again need to find out no of times user logged in last x seconds.
input = {(P1, 0), (P2, 1), (P3, 2), (P1,3), (P1,4), (P2,5), (P1,6)}
Q1: For last 4 seconds for P1 need to output how many times user has logged in.
output: P1,2
Q2: In last 6 seconds for P1:
output: P1,4
To solve this, I have initially used a hashMap, with keys as person id and values as set of timestamps for each person.
Map<personID,set<TimeStamp>> personMap = new HashMap();
personMap.put(P1,Treeset(0,3,4));
personMap.put(P2,Treeset(1,5));
personMap.put(P3,Treeset(2));
then on the final entry, we retrive the treeset and find values that fall after last 4 seconds from given time. This is working, but I am confused on timecomplexity, what will be timecomplexity for inserting and retrival considering values are Treesets?
Is there any better datastructure of storing timestamps in personMap?
The bottom layer of Treeset is red-black tree, and the time complexity of query is O (log n),
Compared with HashSet, TreeSet has slightly lower performance. The time complexity of insert query and other operations is O (log n).
HashMap is recommended.