Interview Question
Country: United States
There can be many approaches to solve it.
As for URL example is concerned we can use the Linear proving mechanism of Hashing. for each URL we can append a new node of timestamp for everytime access.
However generally , the basic many to many relationship can be best represented by Graph ( Directed graph ).
Graph data structure is useful for representing the many to many relationships.
Particularly array representation of directed graph is more suitable to store that data,
where we will use 3D array to represent the relations.
A[No.of URLs][No.of Time Stamps][2]
A[i][j][0] = 1, if URL i is accessed at time stamp j, else 0
A[i][j][1] = 1, if at time stamp j, URL i is accessed, else 0
A graph data structure will best fit this need. To really understand why, start with a single linked list. Assume the head of the list is a given URL, or a given time stamp signifying the start of observation or entries in the log.
- whitenoise April 25, 2013Then the next node that follows will be the next time stamp that the same URL was visited (assuming head = URL). And the next another time stamp. This goes on. When you traverse from the head, you will know that this given URL was accessed in sequence at these time stamps that follow.
Now, start with a time stamp node in the sequence. Branch out of the main link to point to another URL that was accessed at that same time stamp. Repeat the above steps for the URL as the head.
Soon enough, you will end up with a directed graph.