Design on how to store large objects in a list

Viewed 324

I need a hint on an Interview question that I came across. I tried to find a solution but I need advice from experts over here. What are the different strategies you would employ had you came across this particular situation? The question and my thoughts are as follows:

Q. You want to store a huge number of objects in a list in java. The number of objects is very huge and gradually increasing, but you have very limited memory available. How would you do that?

A. I answered by saying that, once the number of elements in the list get over a certain threshold, I would dump them to a file. I would typically then build cache-like data-structure that would hold the most-frequently or recently added elements. I gave an analogy of page swapping employed by the OS.

Q. But this would involve disk access and it would be slower and affect the execution.

I did not know the solution for this and could not think properly during the interview. I tried to answer as:

A. In this case, I would think of horizontally scaling the system or adding more RAM.

Immediately after I answered this, my telephonic interview ended. I felt that the interviewer was not happy with the answer. But then, what should have been the answer.

Moreover, I am not curious only about the answer, I would like to learn about different ways with which this can be handled.

1 Answers

Maybe I am not sure but It indicates that somewhat Flyweight Pattern. This is the same pattern which is been used in String pool and its efficient implementation is must Apart from that, we need focus on database related tasks in order to persist the data if the threshold limit is surpassed. Another technique is to serialize it but as you said, the interviewer was not satisfied and wanted some other explanation.

Related