|I recently lost out on a great job because I couldn't pass the coding challenge. I was given a header file that I COULD NOT modify, and asked to fill it out with a stack with a linked list backbone. The problem was to reverse stack data (integers) such that nodes 1..4 contained 1, 2, 3, 4, and the output would be 4, 3, 2, 1. I easily wrote this, with one problem: none of the methods in the class signatures contained any output method other than top(), which returned the top node. Everything else was a void method. So I modified the size() method t]such that it output the values of each node.
I believe this is where I failed. The code contained a "node" class with a data member and a pointer to the next node. The stack class had a pop method, a push method, that size method, top, and that's about it. Both were templates. Other than adding a friend class or resorting to dirty pointer tricks, I could not figure out how to display the data members of the node class.
I really don't understand how this is a valid problem to pose.
Here is the header I was given:
//a basic stack class.
//Implement all member functions in the file stack.cpp.
// then add a reasonable set of unit tests, with output, to main.cpp.
//Don't modify stack.h or CMakeLists.txt at all, unless you spot a mistake.
template <class t=""> struct _node
_node(const T&, _node<t>*);
template <class t=""> class stack
size_t size() const; //return the size of the stack
T& top() const; //return a reference to the top value. Throw an exception if the stack is empty.
void push(const T&); //push a new value onto the stack
void pop(); //remove the top value from the stack. Do nothing if the stack is empty.
void invert(); //reverse the order of the entire stack, so that 1,2,3,4,5 becomes 5,4,3,2,1 and so on.
//Your function should work in the most effecient way you can devise, ideally without allocating any memory on the heap.