Arista Networks interview question

Question from an Engineer: Design API interface for stack. The stack should be able to take as input a wide variety of data types: it could range from byte sized to an n-byte sized structure

Interview Answer

Anonymous

2 Jul 2011

To create a stack instance, a stack structure has to be defined (struct stack). Also, a separate structure need to be defined (struct stack_node) . HINT1: For linked list based implementations, to make the interface to the stack library as generic as possible, consider the possibility of embedding the stack node into the data structures you wish to insert onto stack and remove from the stack. This way you push the amortization of memory allocation of stack_nodes into the caller's hand. There is no necessity for the copy of information from the calling interface onto the stack library. HINT2: Another possibility is to make the stack interface become aware of the size of the data structures to be inserted. This would involve copy of passed data variables onto the stack node structures leading into further questions like memory allocation overhead and average run time complexities.