Pergunta de entrevista da empresa Tipalti

write a utility that when give a simple object, prints it. a simple object is an object that has only public fields, each field can be of a primitive type (incl. String) or another simple object. follow-up question was how to deal with cyclic referencing between simple objects. when printing should also indent properly the nested objects.

Respostas da entrevista

Sigiloso

17 de set. de 2021

Recurve is a bad solution! Each iteration double the heap

1

Sigiloso

17 de ago. de 2021

in java, iterate over the fields of the provided object using reflection. for each field: if it's primitive or empty - print its name and value. else (it's a nested simple object) make a recursive call on the field value. pass between recursive calls the nesting level (starting with 0 and +1 on each recursive call) so you can properly indent. to be able to break cycles, also pass a set of ref of seen objects, checking before a recursive call that the ref object isn't seen yet. maybe can also break cycles by copying the object to a fresh object before making recursive call?

1