Class Diagnostic {
How would you implement objectSize to return the size in bytes of an object?
The objectSize method returns the size in bytes of all the child nodes combined (each node in the tree).
Example:
Object A (19 bytes) / \ / \ B(20) C(37) / / C(15)
Answer: 19 + 20 + 37 + 15 = 91
I had this question during the interview, and I am very curious to see the answers of others. Since, I knew little about the tree traversal algorithm.
I came up with this ... (I know it is bad or not;), just trying to learn)
public Long objectSize(Object object) { List<Object> objectList = new ArrayList<Object>(); Long sum = sizeOf(object); objectList = getRefs(object); for(Object object : objectList){ sum += objectSize(object); } return sum; }
I noticed that I might have a loop and skip the stackoverflow error, because I did not check if I already went through the "node". Then I hard must have a different data structure (for example, a hash map for processing the key / value) to process the temporary list for comparison.
source share