There are N nodes in a graph connected by exactly N-1 edges. There is exactly 1 shortest path from one node to any other node. The nodes are numbered from 1 to N. Given Q queries which tell source node and the destination nodes. Find the most visited node after traveling those Q paths. For example, say Q=3 and 3 queries are :
1 5
2 4
3 1
So travel from node 1 to node 5, then from node 2 to node 4, then from node 3 to node 1. Finally, find what is the most visited node after the Q queries. Finding every path and incrementing every visited node count is a naive approach. The interviewer asked me to optimize it.
Optimization often involves tradeoffs; in some cases one algorithm is unambiguously better than another, but in other cases one algorithm is better in one respect (e.g. time) and a different algorithm is better in a different respect (e.g. memory usage).
In your case, I'm guessing that your interviewer was looking for an approach that optimizes for the amount of processing that has to be done after you start receiving queries, even if this means you have to do more preprocessing on the graph. My reason for saying this is the term "query"; it's quite common to optimize a data source for "online" querying. (Of course, (s)he probably didn't expect you to decide on your own that this tradeoff was OK; rather, (s)he was likely hoping for a conversation about the different sorts of tradeoffs.)
So, with that in mind . . .
Overall, this requires O(N2) preprocessing, O(Q) realtime processing per query, and O(N) postprocessing.
If N is quite large, and we expect it to be possible that only a small subset of nodes were visited even once, then we can speed up the postprocessing by ignoring unvisited parts of the tree. This involves maintaining a set of nodes that were endpoints of paths, and then doing the postprocessing in "bottom-up" fashion, starting at the deepest such nodes, and moving "parentward" from a given node only if the number of paths that visited that node is less than the number of times it was a lest common ancestor. If we denote the number of distinct endpoints by P and the number of distinct visited nodes by M, then this can be done in something like O(P log P + M).