I've got a real little interesting (at least to me) problem to solve (and, no, it is not homework). It is equivalent to this: you need to determine "sessions" and "sessions start and end time" a user has been on in front of his computer.
You get the time at which any user interaction was made and a maximum period of inactivity. If a time greater or equal than the period of inactivity elapsed between two user inputs, then they are part of different sessions.
Basically the input I get are this (the inputs aren't sorted and I'd rather not sort them before determining the sessions):
06:38
07:12
06:17
09:00
06:49
07:37
08:45
09:51
08:29
And, say, a period of inactivity of 30 minutes.
Then I need to find three sessions:
[06:17...07:12]
[07:37...09:00]
[09:51...09:51]
If the period of inactivity is set to 12 hours, then I'd just find one big session:
[06:17...09:51]
How can I solve this simply?
There's a minimum valid period of inactivity, which shall be about 15 minutes.
The reason I'd rather not sort beforehand is that I'll get a lot of data and only storing them in memory be problematic. However, most of these data shall be part of the same sessions (there shall be relatively few sessions compared to the amount of data, maybe something like thousands to 1 [thousands of user inputs per session]).
So far I am thinking about reading an input (say 06:38) and defining an interval [data-max_inactivity...data+max_inactivity] and, for each new input, use a dichotomic (log n) search to see if it falls in a known interval or create a new interval.
I'd repeat this for every input, making the solution n log n AFAICT. Also, the good thing is that it wouldn't use too much memory for it would only create intervals (and most inputs will fall in a known interval).
Also, every time if falls in a known interval, I'd have to change the interval's lower or upper bound and then see if I need to "merge" with the next interval. For example (for a max period of inactivity of 30 minutes):
[06:00...07:00] (because I got 06:30)
[06:00...07:00][07:45...08:45] (because I later got 08:15)
[06:00...08:45] (because I just received 07:20)
I don't know if the description is very clear, but that is what I need to do.
Does such a problem have a name? How would you go about solving it?
EDIT
I'm very interested in knowing which kind of data structure I should use if I plan to solve it the way I plan to. I need both log n search and insertion/merging ability.