Click here to Skip to main content
15,890,438 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: kernel32.lib Pin
Calin Negru19-May-20 0:02
Calin Negru19-May-20 0:02 
QuestionHow is this possible? Pin
Tim ONeil17-May-20 4:47
Tim ONeil17-May-20 4:47 
AnswerRe: How is this possible? Pin
Richard MacCutchan17-May-20 5:31
mveRichard MacCutchan17-May-20 5:31 
GeneralRe: How is this possible? Pin
Tim ONeil17-May-20 5:39
Tim ONeil17-May-20 5:39 
GeneralRe: How is this possible? Pin
Richard MacCutchan17-May-20 6:28
mveRichard MacCutchan17-May-20 6:28 
GeneralRe: How is this possible? Pin
Richard MacCutchan17-May-20 22:12
mveRichard MacCutchan17-May-20 22:12 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 23:55
mveRichard MacCutchan19-May-20 23:55 
AnswerRe: How is this possible? Pin
Mircea Neacsu19-May-20 3:09
Mircea Neacsu19-May-20 3:09 
Sorry for the late reply, busy day Smile | :)

The good news is that you should be happy you didn't get the job. I'm fairly sure it would have been a miserable work environment. Who in this day and age still hires based on such "code challenges"?

What they forgot is that the code challenge tells as much about the one who wrote it as it does about the one who solves it. In this case:
- have they not heard that names starting with underscores are reserved the implementation? It says: "we are such primadonnas we don't care about frigging standards!"

- you have _node(const T&, _node<t>*); Who is "T" and "t"? You should at least bother to run your header through a compiler to see you that you don't have any syntax errors. If you are at it, look also for a spell-checker: it's "efficient" not "effecient". This shows lack of respect for the person you are interviewing.

- template <class t=""> struct _node Seriously? Show me the compiler that doesn't flag it as an error.

- the instructions suck also: "Implement all member functions in the file stack.cpp" Have you not heard that template implementations are normally in the same .h file? If I put it in stack.cpp, then in main.cpp I have to include "stack.cpp". Yey!

The code itself is fairly bland. I suspect they wanted to see you do some pointer acrobatics in the "invert" member function:

template<class T>
_node<T>::_node (const T& dat, _node<T>* ptr) :
  _data (dat),
  _next (ptr)
{
}

template<class T>
_node<T>::~_node ()
{
}

template<class T>
stack<T>::stack () :
  _size (0),
  _head (nullptr)
{
}


template<class T>
stack<T>::~stack ()
{
  while (_head)
  {
    _node<T>* ptr = _head->_next;
    delete _head;
    _head = ptr;
  }
}

template<class T>
size_t stack<T>::size () const
{
  return _size;
}

template<class T>
T& stack<T>::top () const
{
  if (!_size)
    throw std::exception ("empty_stack");

  return _head->_data;
}

template<class T>
void stack<T>::push (const T& data)
{
  _node<T>* node = new _node<T> (data, _head);
  _head = node;
  _size++;
}

template<class T>
void stack<T>::pop ()
{
  if (!_size)
    return;

  _node<T>* ptr = _head->_next;
  delete _head;
  _head = ptr;
  --_size;
}

template<class T>
void stack<T>::invert ()
{
  _node<T>* prev = nullptr;
  _node<T>* crt = _head;
  while (crt)
  {
    _node<T>* next = crt->_next;
    crt->_next = prev;
    prev = crt;
    crt = next;
  }
  _head = prev;
}


I don't want to go on here talking about the dangers of not providing a copy constructor when you are knee-deep in pointer manipulations, not respecting the "big three" (or big five) rule, and so on and so forth.

For the "unit test" part you can write something like this:
int main (int argc, char** argv)
{
  stack<int> the_stack;

  cout << "Empty stack has size " << the_stack.size () << endl;
  
  the_stack.push (1);
  the_stack.push (2);
  the_stack.push (3);
  the_stack.push (4);
  the_stack.push (5);
  cout << "5 elements pushes, size is " << the_stack.size () << endl;
  cout << "Top element is " << the_stack.top () << endl;

  the_stack.invert ();
  cout << "Stack inverted elements are (from top):" << endl;
  int n = the_stack.size ();
  for (int i = 0; i < n; i++)
  {
    cout << "Element " << i + 1 << " is " << the_stack.top () << endl;
    the_stack.pop ();
  }

  return 0;
}


To wrap it up, my advice would be to look for an environment where they hire based on your work portfolio, where they understand that a coder must be also a person with good communication skills and with a good understanding of the problem domain. Build a portfolio of code writing high-quality code and learning from masters (Knuth, Wirth, Djikstra and so many others). Remember: if you cannot write your code to read like a poem, make it at least like a novel.
Mircea

PraiseRe: How is this possible? Pin
Greg Utas19-May-20 3:37
professionalGreg Utas19-May-20 3:37 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 3:37
mveRichard MacCutchan19-May-20 3:37 
GeneralRe: How is this possible? Pin
Mircea Neacsu19-May-20 3:57
Mircea Neacsu19-May-20 3:57 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 4:16
mveRichard MacCutchan19-May-20 4:16 
GeneralRe: How is this possible? Pin
Mircea Neacsu19-May-20 5:08
Mircea Neacsu19-May-20 5:08 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 5:24
mveRichard MacCutchan19-May-20 5:24 
GeneralRe: How is this possible? Pin
Mircea Neacsu19-May-20 6:07
Mircea Neacsu19-May-20 6:07 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 21:36
mveRichard MacCutchan19-May-20 21:36 
GeneralRe: How is this possible? Pin
Mircea Neacsu20-May-20 2:40
Mircea Neacsu20-May-20 2:40 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 23:22
mveRichard MacCutchan19-May-20 23:22 
GeneralRe: How is this possible? Pin
Mircea Neacsu20-May-20 2:44
Mircea Neacsu20-May-20 2:44 
GeneralRe: How is this possible? Pin
Richard MacCutchan19-May-20 4:18
mveRichard MacCutchan19-May-20 4:18 
AnswerRe: How is this possible? Pin
Richard MacCutchan19-May-20 21:38
mveRichard MacCutchan19-May-20 21:38 
QuestionQueryPerformanceCounter Pin
Calin Negru16-May-20 2:16
Calin Negru16-May-20 2:16 
AnswerRe: QueryPerformanceCounter Pin
Greg Utas16-May-20 2:29
professionalGreg Utas16-May-20 2:29 
GeneralRe: QueryPerformanceCounter Pin
Calin Negru16-May-20 3:11
Calin Negru16-May-20 3:11 
AnswerRe: QueryPerformanceCounter Pin
Richard MacCutchan16-May-20 2:58
mveRichard MacCutchan16-May-20 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.